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. 一条经典SQL语句优化实例

    1.概述 如下SQL语句发生严重消耗资源的问题,使得OS's load average会在30以上,一条语句需要执行上百秒. /*PIXPatient 184176条DomainPatient 184 ...

  2. c配置库ccl使用小结

    配置文件为key=value键值对形式 下载与安装 库文件下载:ccl-0.1.1.tar.gz 安装:  tar -zxvf ccl-0.1.1.tar.gz  cd ccl-0.1.1 ./con ...

  3. 第二百五十六节,Web框架

    Web框架 Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 举例: #!/usr/bin/env python #c ...

  4. 什么是Mybatis

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...

  5. 一个区域只能放置一个组件,如果想在一个区域放置多个组件就需要使用Panel来装载

    五种布局管理器: Flow Layout(流式布局):按照组件添加到容器中的顺序,顺序排放组件位置.默认为水平排列,如果越界那么会向下排列.排列的位置随着容器大小的改变而改变. Panel默认的布局管 ...

  6. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...

  7. npoi导出excel_asp.net MVC

    下载路径: http://files.cnblogs.com/files/gaocong/npoi_for_mysql_mvc.rar

  8. maven环境的配置

    http://maven.oschina.net/help.html     --配置说明 http://maven.oschina.net/index.html#nexus-search;quick ...

  9. 【转】Native Thread for Win32 C- Creating Processes(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32C.php To create a new process, we need to ca ...

  10. javascript new Date()函数在不同浏览器上返回不同的值

    今天是2015年3月16日,在IE 6 上new Date()返回给后台server的字符串格式为 "Mon Mar 16  00:00:00 UTC 0800 2015",而在f ...