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. Ubuntu 下添加OpenERP command 快捷启动方式

    编辑home目录下的.bashrc文件 alias xjerp="~/odoo/xj/openerp-server -r openerp --addons-path='~/odoo/xj/o ...

  2. Spring.Net框架二:配置Spring.Net框架环境

    一.下载DLL文件 去Spring的官方网站下载并解压,然后直接添加dll文件的引用就可以了.在上一篇文章中,已经介绍过Spring.Net框架中需要使用到的dll文件.这些程序集文件位于Spring ...

  3. 微信小程序 - toptip效果

    在Page顶部下滑一个提示条 , 代码见 /mixins/UIComponent.js ,其中的self 可以认为是微信小程序的Page对象 效果: 默认2秒展示,上移动画隐藏 /** * 展示顶部 ...

  4. 清除Css中select的下拉箭头样式

    select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...

  5. 我的vim 自动实例括号函数

    不废话,直接上代码: """"""""""""""" ...

  6. 使用css全面美化input标签

    做网站时经常有这样那样的需要,要美化input ,于是CSS的美化必不可少.和程序人生的站长交流,他发给我这个. 下面是CSS样式 input { border:1px solid #B3D6EF; ...

  7. Javascript代码执行过程-《悟透Javascript》笔记

    本文摘录自李战老师<悟透Javascript>一书的部分章节,为适应博客发表作了一点点修改. 1) 预编译分析. JavaScript执行引擎将所有定义式函数直接创建为作用域上的函数变量, ...

  8. 配置sudo su

    买了UCloud的机器默认给的是root权限,从安全考虑,这个得改改,那就添加一个普通用户吧.. 可是那群民工又有话说了,得有root权限才能启动那些服务进程,每次都要输入root密码才能切换到roo ...

  9. SVN入门 服务器VisualSVN Server和客户端TortoiseSVN安装

    Subversion是一个版本控制系统,相对于的RCS.CVS,采用了分支管理系统,它的设计目标就是取代CVS.互联网上免费的版本控制服务多基于Subversion. 一.SVN工作原理 SVN(Su ...

  10. Apache版Phoenix的安装(图文详解)

    不多说,直接上干货! 写在前面的话 我这里,三个节点的bigdata集群.分别为master.slave1和slave2. 1.Phoenix的下载 我的HBase版本是hbase-0.98.19. ...