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 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 #include<vector>
7 #include<string.h>
8 #include<math.h>
9 #include<map>
10 #include<vector>
11 char str1[20];
12 char str2[20];
13 int cnt[200005];
14 int top(int ans,int nn,int k);
15 using namespace std;
16 vector<int>vec[20005];
17 int main(void)
18 {
19 int i,j,k,p,q;
20 int s;
21 scanf("%d",&k);
22 for(s=1; s<=k; s++)
23 {
24 map<string,int>my;
25 memset(cnt,0,sizeof(cnt));
26 scanf("%d",&p);
27 int ans=1;
28 for(i=0; i<20004; i++)
29 vec[i].clear();
30 for(i=0; i<p; i++)
31 {
32 scanf("%s %s",str1,str2);
33 if(my[str1]==0)
34 {
35 my[str1]=ans++;
36 }
37 if(my[str2]==0)
38 {
39 my[str2]=ans++;
40 }
41 vec[my[str1]].push_back(my[str2]);
42 cnt[my[str2]]++;
43 }
44 int ss=top(ans-1,0,ans-1);
45 printf("Case %d: ",s);
46 if(ss)printf("Yes\n");
47 else printf("No\n");
48 }
49 return 0;
50 }
51 int top(int ans,int nn,int k)
52 {
53 int i,j;
54 queue<int>que;
55 for(i=1; i<=ans; i++)
56 if(cnt[i]==0)
57 {
58 nn++;
59 que.push(i);
60 }
61 while(!que.empty())
62 {
63 int y=que.front();
64 que.pop();
65 for(i=0; i<vec[y].size(); i++)
66 {
67 cnt[vec[y][i]]--;
68 if(cnt[vec[y][i]]==0)
69 {
70 nn++;
71 que.push(vec[y][i]);
72 }
73 }
74 }
75 if(nn==k)return 1;
76 else return 0;
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. 【模板】单源最短路径(Dijkstra)/洛谷P4779

    题目链接 https://www.luogu.com.cn/problem/P4779 题目大意 给定一个 \(n\) 个点 \(m\) 条边有向图,每个点有一个非负权值,求从 \(s\) 点出发,到 ...

  2. Vector总结及部分底层源码分析

    Vector总结及部分底层源码分析 1. Vector继承的抽象类和实现的接口 Vector类实现的接口 List接口:里面定义了List集合的基本接口,Vector进行了实现 RandomAcces ...

  3. tensorboard 拒绝连接无法打开相应页面

    启动tensorboard时没有报错,但打开页面却拒绝连接. 解决方法:tensorboard --logdir=TEC4FN --host=127.0.0.1 在命令最后添加 --host=127. ...

  4. YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

    YARP 是微软开源的一个反向代理项目,英文名叫 Yet Another Reverse Proxy .所谓反向代理最有名的那就是 nginx 了,没错 YARP 也可以用来完成 nginx 的大部分 ...

  5. hadoop/spark面试题

    总结于网络 转自:https://www.cnblogs.com/jchubby/p/5449379.html 1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文 ...

  6. map/multimap深度探索

    map/multimap同样以rb_tree为底层结构,同样有元素自动排序的特性,排序的依据为key. 我们无法通过迭代器来更改map/multimap的key值,这个并不是因为rb_tree不允许, ...

  7. Rust 总章

    1.1 Rust安装 3.5 Rust Generic Types, Traits, and Lifetimes 3.6 String 与 切片&str的区别 https://openslr. ...

  8. gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法

    1. Right click on the project and go to "Properties" //鼠标右键点击项目,然后选中Properties   2. Select ...

  9. JDBC(2):JDBC对数据库进行CRUD

    一. statement对象 JDBC程序中的Connection用于代表数据库的链接:Statement对象用于向数据库发送SQL语句:ResultSet用于代表Sql语句的执行结果 JDBC中的s ...

  10. SpringBoot项目找不到主类或无法加载主类

    问题描述 启动springboot项目的时候发现启动失败,查看日志发现因为找不到主类或无法加载主类. 解决 我这个项目是拉取的别人git上的项目,看了一下目录结构发现没有编译后的文件(target目录 ...