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

拓扑排序,判断是否有环。

嗯...只要剩下点就是有环 好久没看自己以前 写的博客了http://fengweiding.blog.163.com/blog/static/2300541212014112831155847/

/* ***********************************************
Author :guanjun
Created Time :2016/6/7 20:22:01
File Name :1003.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
vector<int>edge[maxn];
int m,num;
map<string,int>mp;
int in[maxn];
bool topsort(){
int cnt=;
queue<int>q;
for(int i=;i<=num;i++)if(in[i]==)q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
cnt++;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
in[v]--;
if(in[v]==){
q.push(v);
}
}
}
if(cnt<num)return false;
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T;
string s1,s2;
cin>>T;
for(int t=;t<=T;t++){
cin>>m;
num=;mp.clear();cle(in);
for(int i=;i<maxn;i++)edge[i].clear();
for(int i=;i<=m;i++){
cin>>s1>>s2;
if(!mp[s1])mp[s1]=++num;
if(!mp[s2])mp[s2]=++num;
edge[mp[s1]].push_back(mp[s2]);
in[mp[s2]]++;
}
printf("Case %d: %s\n",t,topsort()?"Yes":"No");
}
return ;
}

最新文章

  1. Android安全开发之ZIP文件目录遍历
  2. Oracle中三种循环(For、While、Loop)
  3. Android 2.x中使用actionbar - Actionbarsherlock
  4. batch_size 和 fetch_size作用
  5. Git补丁
  6. LA 4287
  7. 基础学习总结(三)--文本、SD卡数据读写
  8. 团体程序设计天梯赛-练习集L1-012. 计算指数
  9. hdu 4752
  10. 基于.NET平台常用的框架和开源程序整理
  11. Codeforces Jzzhu and Sequences(圆形截面)
  12. 最新IP数据库 存储优化 查询性能优化 每秒解析上千万
  13. djanogo class meta
  14. Vue+koa2开发一款全栈小程序(8.图书列表页)
  15. 在linux系统中出现u盘问题 的相关解决方法
  16. elasticsearch 口水篇(3)java客户端 - Jest
  17. QT隐式数据共享
  18. 数据库 —— mySQL相关
  19. Python 正则表达式贪婪模式
  20. T-SQL备忘(6):常用内置函数

热门文章

  1. 【HIHOCODER 1478】 水陆距离(BFS)
  2. ACM模板(Java)
  3. ServletContext作用功能详解
  4. XTU 二分图和网络流 练习题 C. 方格取数(1)
  5. Flask---ajax(jquery)交互
  6. Go常量与枚举类型
  7. [luoguP2618] 数字工程(DP)
  8. [Tyvj1939] 玉蟾宫(单调栈)
  9. 反编译sencha toucha打包的apk文件,修改应用名称支持中文以及去除应用标题栏
  10. Easy sssp(vijos 1053)