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基础总结(三)SQLite,ListView,对话框

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  2. 大数据(11) - kafka的安装与使用

    一.Kafka概述 1.Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成.是由 ...

  3. orm工具的基本思想

    orm工具的基本思想无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.2. 由sessionf ...

  4. LodRunner实现大负载测试的四部曲(配置系统参数、配置LR、修改脚本、设置组策略)

    见 http://www.51testing.com/?uid-97659-action-viewspace-itemid-210924 LoadRunner以下简称(LR)是目前业界最流行的压力测试 ...

  5. 如何用ChemDraw绘制化学课件

    近年来随着ChemDraw等多媒体技术的迅速发展,多媒体技术越来越多的应用在教学中.学会应用ChemDraw绘制化学分子结构.化学反应式和实验装置的方法,将在有机化学的教学中提供一定的帮助,进一步提高 ...

  6. UESTC 1511(差分约束)

    题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...

  7. Spring Cache 自定义注解

    1.在使用spring cache注解如cacheable.cacheevict.cacheput过程中有一些问题: 比如,我们在查到一个list后,可以将list缓存到一个键对应的区域里:当新增.修 ...

  8. JNI 各类数据类型处理

    JNI和java数据类型转换: 1.基本数据类型下面一张表是描述了 Java 基本数据类型和JNI中基本数据类型的相对应关系已经占用空间大小. 随便观察就能发现,其实就基本数据类型而已,JNI基本数据 ...

  9. 安装wampserver时提示丢失MSVCR110.dll

    安装Wampserver 2后启动的时候提示系统错误:MSVCR110.dll丢失. 在wampserver官网上有例如以下提示: 于是卸载原来的WAMPSERVER 2 ,在http://www.m ...

  10. shell脚本学习总结06--数学计算

    在bash中可利用let,(())和[]执行基本的操作,高级操作将会使用expr和bc 运算符:+,—,*,/,**(幂) (()) [root@Director ~]# ((c=2**3-9%2)) ...