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. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. reactNative 介绍

    React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是Facebook早先开源的UI框架 React 在原生移动应用平台的衍生产物,目前支持iOS和安 ...

  3. ztree2.6给菜单增加title提示信息[转]

    自定义数据格式的情况下(isSimpleData: true) 在setting中自定义一个属性如 remark:"remark", callback中调用函数 nodeCreat ...

  4. SQL中distinct的用法(四种示例分析)

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只 用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...

  5. ESI 动态缓存技术[转载]

    任何一个Web网站的内容都是在不断更新和变化,但这并不意味这这个网站的内容就是动态内容,事实上,动态的内容是指用户每次点击 相同的链接时取的的内容是由Web服务器应用程序生成的,如常见得ASP,JSP ...

  6. centos 中找不到 php-config

    两种解决方式: 1.CentOS6 PHP extension install: Cannot find php-config. Please use --with-php-config=PATH 2 ...

  7. Java并发包——Atomic操作

    Java并发包——Atomic操作 摘要:本文主要学习了Java并发包下的atomic包中有关原子操作的一些类. 部分内容来自以下博客: https://blog.csdn.net/qq_303796 ...

  8. maven之基础

    一.配置maven的环境 下载地址:http://maven.apache.org/download.cgi(window下载(Binary zip)的) 将maven解压,然后配置环境变量打开: 计 ...

  9. ntfs格式uefi启动u盘

    http://www.laomaotao.org/softhelp/syjc/925.html http://www.laomaotao.org/softhelp/wtjd/989.html http ...

  10. struts2 input file多文件同时通过ajax提交

    <input type="file" name="files" multiple="multiple">必须是multiple才 ...