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

拓扑排序判环

/*************************************************************************
> 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[N], 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[N]; 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;
}

LightOJ1003---Drunk(拓扑排序判环)的更多相关文章

  1. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  2. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  3. HDU1811 拓扑排序判环+并查集

    HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...

  4. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  5. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  6. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  7. HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】

    Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

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

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

随机推荐

  1. Android开发之Fragment传递參数的几种方法

    Fragment在Android3.0開始提供,而且在兼容包中也提供了Fragment特性的支持. Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自己定义Fragmen ...

  2. VMware12激活码,win10激活码

    VMware Workstation 12序列号: 5A02H-AU243-TZJ49-GTC7K-3C61N win10激活码:这里在网上搜集到很多激活码,可能有的不能用.   WRUF7-AFI0 ...

  3. Android基础总结(九)多媒体

    多媒体概念(了解) 文字.图片.音频.视频 计算机图片大小的计算(掌握) 图片大小 = 图片的总像素 * 每个像素占用的大小 单色图:每个像素占用1/8个字节 16色图:每个像素占用1/2个字节 25 ...

  4. Hadoop-2.2.0中文文档—— MapReduce 下一代 - Encrypted Shuffle

    简单介绍 Encrypted Shuffle capability (加密洗牌功能? )同意用HTTPS 和 可选的client验证 (也称作双向的 HTTPS, 或有client证书的 HTTPS) ...

  5. Spring mvc 返回JSON 在IE 下提示下载 解决办法

    http://www.blogjava.net/iamlibo/archive/2013/11/21/406646.html ————————————————————————————————————— ...

  6. C++ 构造函数的对象初始化列表

    //构造函数的对象初始化列表 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class P ...

  7. 解决阿里云部署 office web apps ApplicationFailedException 报错问题

    查找这个问题,确实花费了很长时间,所以具体解析一下问题原因吧. 报错如下: 问题详情链接 New-OfficeWebAppsFarm:Office Online服务无法启动.有关详细信息,请参阅Win ...

  8. JSON美化输出

    echo '{"a": 1, "b": 2}' | python -m json.tool 转自: http://blog.csdn.net/chosen0ne ...

  9. VBS 处理断开excel数据链接格式,只保留值

    最近有个项目是将一个excel压缩之后发给客户,但是由于excel数据过大,即使压缩之后仍然接近5M,因为是大批量发送数据,所以非常慢.急需要将EXCEL数据压缩. 后来我想到一个办法,就excel数 ...

  10. ASP.NET MVC:Expression Trees 作为参数简化查询

    ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力.在查询 Action ...