One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let me share his ideas a bit. I am expressing in my words.

There are many kinds of drinks, which he used to take. But there are some rules; there are some drinks that have some pre requisites. Suppose if you want to take wine, you should have taken soda, water before it. That's why to get real drunk is not that easy.

Now given the name of some drinks! And the prerequisites of the drinks, you have to say that whether it's possible to get drunk or not. To get drunk, a person should take all the drinks.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with an integer m (1 ≤ m ≤ 10000). Each of the next m lines will contain two names each in the format a b, denoting that you must have a before havingb. The names will contain at most 10 characters with no blanks.

Output

For each case, print the case number and 'Yes' or 'No', depending on whether it's possible to get drunk or not.

Sample Input

Output for Sample Input

2

2

soda wine

water wine

3

soda wine

water wine

wine water

Case 1: Yes

Case 2: No


PROBLEM SETTER: JANE ALAM JAN:
思路:直接拓扑排序判断有无环,复杂度O(N+E)
  1. 1 #include<stdio.h>
  2. 2 #include<stdlib.h>
  3. 3 #include<iostream>
  4. 4 #include<algorithm>
  5. 5 #include<queue>
  6. 6 #include<vector>
  7. 7 #include<string.h>
  8. 8 #include<math.h>
  9. 9 #include<map>
  10. 10 #include<vector>
  11. 11 char str1[20];
  12. 12 char str2[20];
  13. 13 int cnt[200005];
  14. 14 int top(int ans,int nn,int k);
  15. 15 using namespace std;
  16. 16 vector<int>vec[20005];
  17. 17 int main(void)
  18. 18 {
  19. 19 int i,j,k,p,q;
  20. 20 int s;
  21. 21 scanf("%d",&k);
  22. 22 for(s=1; s<=k; s++)
  23. 23 {
  24. 24 map<string,int>my;
  25. 25 memset(cnt,0,sizeof(cnt));
  26. 26 scanf("%d",&p);
  27. 27 int ans=1;
  28. 28 for(i=0; i<20004; i++)
  29. 29 vec[i].clear();
  30. 30 for(i=0; i<p; i++)
  31. 31 {
  32. 32 scanf("%s %s",str1,str2);
  33. 33 if(my[str1]==0)
  34. 34 {
  35. 35 my[str1]=ans++;
  36. 36 }
  37. 37 if(my[str2]==0)
  38. 38 {
  39. 39 my[str2]=ans++;
  40. 40 }
  41. 41 vec[my[str1]].push_back(my[str2]);
  42. 42 cnt[my[str2]]++;
  43. 43 }
  44. 44 int ss=top(ans-1,0,ans-1);
  45. 45 printf("Case %d: ",s);
  46. 46 if(ss)printf("Yes\n");
  47. 47 else printf("No\n");
  48. 48 }
  49. 49 return 0;
  50. 50 }
  51. 51 int top(int ans,int nn,int k)
  52. 52 {
  53. 53 int i,j;
  54. 54 queue<int>que;
  55. 55 for(i=1; i<=ans; i++)
  56. 56 if(cnt[i]==0)
  57. 57 {
  58. 58 nn++;
  59. 59 que.push(i);
  60. 60 }
  61. 61 while(!que.empty())
  62. 62 {
  63. 63 int y=que.front();
  64. 64 que.pop();
  65. 65 for(i=0; i<vec[y].size(); i++)
  66. 66 {
  67. 67 cnt[vec[y][i]]--;
  68. 68 if(cnt[vec[y][i]]==0)
  69. 69 {
  70. 70 nn++;
  71. 71 que.push(vec[y][i]);
  72. 72 }
  73. 73 }
  74. 74 }
  75. 75 if(nn==k)return 1;
  76. 76 else return 0;
  77. 77 }

LightOJ - 1003 Drunk的更多相关文章

  1. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  2. Lightoj 1003 - Drunk(拓扑排序)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  3. Loj 1003–Drunk(拓扑排序)

    1003 - Drunk PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB One of my fr ...

  4. lightoj 1003

    有向图拓扑排序,判段是否存在. #include<map> #include<cstdio> #include<string> #include<cstrin ...

  5. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  6. lightoj 1381 - Scientific Experiment dp

    1381 - Scientific Experiment Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lightoj.com/vo ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. Bestcoder#5 1003

    Bestcoder#5 1003 Poor RukawTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. lxml解析库的安装和使用

    一.lxml的安装lxml是Python的一个解析库,支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高.本节中,我们了解一下lxml的安装方式,这主要从Windows.Linux ...

  2. 省时省心DTM,广告转化无难题

    内容来源:华为开发者大会2021 HMS Core 6 App Services技术论坛,主题演讲<华为分析服务,助您打造数智化运营闭环方案>. 演讲嘉宾:华为消费者云服务 分析产品总监 ...

  3. Freeswitch 安装爬坑记录1

    2 Freeswitch的安装 2.1 准备工作 服务器安装CentOS 因为是内部环境,可以关闭一些防火墙设置,保证不会因为网络限制而不能连接 关闭防火墙 查看防火墙 systemctl statu ...

  4. 巩固javaweb第九天

    巩固内容: HTML <base> 元素 <base> 标签描述了基本的链接地址/链接目标,该标签作为HTML文档中所有的链接标签的默认链接: <head> < ...

  5. day9 图书设计项目

    总路由层url from django.conf.urls import url from django.contrib import admin from app01 import views ur ...

  6. day13 装饰器与语法糖

    day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...

  7. Hive(十三)【Hive on Spark 部署搭建】

    Hive on Spark 官网详情:https://cwiki.apache.org//confluence/display/Hive/Hive+on+Spark:+Getting+Started ...

  8. 高效读取大文件,再也不用担心 OOM 了!

    内存读取 第一个版本,采用内存读取的方式,所有的数据首先读读取到内存中,程序代码如下: Stopwatch stopwatch = Stopwatch.createStarted(); // 将全部行 ...

  9. 【编程思想】【设计模式】【结构模式Structural】front_controller

    Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bi ...

  10. lucene中创建索引库

    package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...