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. MongoDB助力快速搭建物流订单系统

    简介 快递物流系统里最常见的一种业务类型就是订单的查询和记录.订单的特点是随着递送过程,订单数据需要随时更新路径.数据结构上需要可以灵活应对,这点非常符合Document模型,并且MongoDB支持G ...

  2. Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法

    Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法   LBS 球面距离公式 http://wiki.myoa.info/zh-blog:20 Java,Mysql- ...

  3. 什么是Mybatis

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

  4. 遍历一个Set的方法只有一个:迭代器(interator)

    Set-HashSet实现类: 遍历一个Set的方法只有一个:迭代器(interator). HashSet中元素是无序的(这个无序指的是数据的添加顺序和后来的排列顺序不同),而且元素不可重复. 在O ...

  5. 如果将一个类设置为abstract,则此类必须被继承使用

    利用final定义方法:这样的方法为一个不可覆盖的方法. Public final void print(){}: 为了保证方法的一致性(即不被改变),可将方法用final定义. 如果在父类中有fin ...

  6. [ACM] FZU 2086 餐厅点餐 (枚举)

    roblem Description Jack近期喜欢到学校餐厅吃饭.好吃干净还廉价. 在学校餐厅.有a种汤,b种饭.c种面条,d种荤菜,e种素菜. 为了保证膳食搭配,Jack每顿饭都会点1~2样荤菜 ...

  7. 使用命令行操控VirtualBox虚拟机

    (1)启动虚拟机:$ VBoxManage startvm <VMNAME> --type gui  #执行结束后,就会启动指定的虚拟机,几乎和平时没什么区别. $ VBoxManage ...

  8. 【NLP+Deep learning】好文

    http://blog.jobbole.com/77709/ 原文出处: http://colah.github.io/posts/2014-07-NLP-RNNs-Representations/

  9. OSG设置警告等级

    osg::setNotifyLevel(osg::FATAL);//控制台只输出严重错误信息

  10. 第十四篇:Apriori 关联分析算法原理分析与代码实现

    前言 想必大家都听过数据挖掘领域那个经典的故事 - "啤酒与尿布" 的故事. 那么,具体是怎么从海量销售信息中挖掘出啤酒和尿布之间的关系呢? 这就是关联分析所要完成的任务了. 本文 ...