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 ;
}

Lightoj 1003 - Drunk(拓扑排序)的更多相关文章

  1. Loj 1003–Drunk(拓扑排序)

    1003 - Drunk PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB One of my fr ...

  2. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  3. LightOJ - 1003 Drunk

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  4. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  5. HDU2094产生冠军 (拓扑排序)

    HDU2094产生冠军 Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛. 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认 ...

  6. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  7. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  8. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  9. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

随机推荐

  1. 大数据学习——mapreduce汇总手机号上行流量下行流量总流量

    时间戳 手机号 MAC地址 ip 域名 上行流量包个数 下行 上行流量 下行流量 http状态码 1363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 12 ...

  2. CentOS7 设置代理

    大多数公司的网络都使用局域网加代理上网,也就是说上外网必须使用公司指定的代理服务器,这有几个好处: 1. 首先代理可以一定程度提高浏览速度,因为可以将更多的网页缓存在代理服务器上,需要的时候直接拿就很 ...

  3. Oracle臨時表空間過大問題解決

    查詢資料庫伺服器時,發現資料庫伺服器磁片使用空間達到了98%,分析總共的資料檔案也不可能達到如此大,經過查詢發現原來臨時表空間的使用方式達到了 32G,導致磁碟空間使用緊張.搜索了相應的文檔與資料後, ...

  4. FZU 1686 dlx重复覆盖

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  5. 汕头市赛srm10 T2

    n个数,分组,数Ai要在至少含有Ai个数的组,求最多分多少组. 方法一:大的数应该尽量跟大的在一起,这样才能让小的出现很多很多组,所以从大到小排序,给当前序列中最大的数x分x个数.代码如下: #inc ...

  6. SeaJS项目完整实例【转】

    index.html——主页面. sea.js——SeaJS脚本. init.js——init模块,入口模块,依赖data.jquery.style三个模块.由主页面载入. data.js——data ...

  7. jquery的ajax提交时“加载中”提示的处理方法

    方法1:使用ajaxStart方法定义一个全局的“加载中...”提示 $(function(){    $("#loading").ajaxStart(function(){    ...

  8. poj2767,单向连通图判定,缩点+重新建图+新图DFS

    /*该题被博客里标记为中等题,30分钟内1A,掌握了算法就简单了,单向连通图判定,单向连通图缩点 后必然唯一存在出度为0的点和入度为0的点,并且从入度为0的点出发,可以遍历所有点后到达出度为0点 (一 ...

  9. CF821E(多次矩阵快速幂)

    题意: 冈伦从二维平面上(0,0)走到(k,0),(k<=1e18),每次有三个行动方向:右上一格.右方一格.右下一格,问一共有多少种走的方案 限制:每段x都有一个天花板,一共有n段天花板(n& ...

  10. cppcon

    https://cppcon.org/ https://github.com/cppcon