LightOJ1003---Drunk(拓扑排序判环)
2015-06-03 10:38
381 查看
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 having b. 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
拓扑排序判环
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 having b. 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
拓扑排序判环
/************************************************************************* > File Name: LightOJ1003.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年06月03日 星期三 10时19分43秒 ************************************************************************/ #include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <map> #include <bitset> #include <set> #include <vector> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long long LL; typedef pair <int, int> PLL; static const int N = 10100; struct node { int nxt; int to; }edge[N + 10]; int head , tot; void addedge(int from, int to) { edge[tot].to = to; edge[tot].nxt = head[from]; head[from] = tot++; } map <string, int> mp; char A[20], B[20]; int in_deg ; void toposort(int n) { queue <int> qu; int rest = n; for (int i = 1; i <= n; ++i) { if (!in_deg[i]) { qu.push(i); } } while (!qu.empty()) { int u = qu.front(); qu.pop(); --rest; for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].to; --in_deg[v]; if (!in_deg[v]) { qu.push(v); } } } if (rest) { printf("No\n"); } else { printf("Yes\n"); } } int main() { int t, icase = 1; scanf("%d", &t); while (t--) { mp.clear(); int m; scanf("%d", &m); memset(head, -1, sizeof(head)); tot = 0; int n = 0; memset(in_deg, 0, sizeof(in_deg)); for (int i = 1; i <= m; ++i) { scanf("%s%s", A, B); if (mp.find(A) == mp.end()) { mp[A] = ++n; } if (mp.find(B) == mp.end()) { mp[B] = ++n; } ++in_deg[mp[B]]; addedge(mp[A], mp[B]); } printf("Case %d: ", icase++); toposort(n); } return 0; }
相关文章推荐
- Linux常用网络命令(二)
- 关闭Eclipse的控制台console自动跳出
- 如何限制oracle的trace files及alert file大小
- 字节序(大小端)详解从高低地址和高低位开始理解
- 商品推荐算法-协同过滤
- 【基础】GNS3 给路由器添加板卡及基本操作
- 多目录多文件批量解压
- 精通 JS正则表达式
- ant 指南
- Codeforces #285 Div 1 简要题解
- 7zip压缩整个目录
- Android图片异步加载框架Android-Universal-Image-Loader
- eclipse4.2+安装modelgoon插件,该插件支持在eclipse直接根据java文件生产类图
- oracle模糊查询语句使用示例
- 自定义回调 并将之以参数的形式传入
- svn使用
- STL系列之三 queue 单向队列
- DELL 8700重装系统
- python 日期转星期
- 如何自学图像编程