1003 - Drunk


PDF (English)
Statistics
Forum

Time Limit: 2 second(s)
Memory Limit: 32 MB

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 most10 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

2

2

soda wine

water wine

3

soda wine

water wine

wine water

Output for Sample Input

Case 1: Yes

Case 2: No

::学习一个新算法,总没那么容易,今天又因为一个弄错一个字母找了半天*.*!!

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <algorithm>

   4: #include <cstring>

   5: #include <map>

   6: using namespace std;

   7: const int maxn=11000;

   8: int head[maxn],in[maxn];

   9: bool vis[maxn];

  10: int ant,cas=1,id;

  11:  

  12: map<string,int>a;

  13: struct EDGE{

  14:     int v, next;

  15:     EDGE(){}

  16:     EDGE(int _v, int _next){v = _v, next = _next;}

  17: }e[maxn];

  18: int ecnt;

  19:  

  20: void add(int u, int v){

  21:     e[ecnt] = EDGE(v, head[u]);

  22:     head[u] = ecnt++;

  23: }

  24:  

  25: bool topo(){

  26:     memset(vis, 0, sizeof(vis));

  27:  

  28:     for(int t = 0; t < id; t++){

  29:         int u;

  30:         for(u = 0; u < id; u++)

  31:             if(!vis[u] && !in[u])

  32:                 break;

  33:         if(u >= id)

  34:             return false;

  35:  

  36:         vis[u] = true;

  37:         in[u]--;

  38:         for(int i = head[u]; i != -1; i = e[i].next){

  39:             int v = e[i].v;

  40:             in[v]--;

  41:         }

  42:     }

  43:     return true;

  44: }

  45:  

  46: void solve()

  47: {

  48:     int n;

  49:     ecnt=0,id=0;

  50:     a.clear();

  51:     memset(head,-1,sizeof(head));

  52:     memset(in,0,sizeof(in));

  53:     cin>>n;

  54:  

  55:     while(n--)

  56:     {

  57:         string x,y;

  58:         cin>>x>>y;

  59:  

  60:         if(a.find(x)==a.end()) a[x]=id++;

  61:         if(a.find(y)==a.end()) a[y]=id++;

  62:         int u=a[x],v=a[y];

  63:         add(u,v);

  64:         in[v]++;

  65:     }

  66:     if(topo())

  67:         cout<<"Yes"<<endl;

  68:     else

  69:         cout<<"No"<<endl;

  70: }

  71:  

  72: int main()

  73: {

  74:     ios::sync_with_stdio(false);

  75:     int t, cas = 0;

  76:     cin>>t;

  77:     while(t--){

  78:         cout<<"Case "<< (++cas)<<": ";

  79:         solve();

  80:     }

  81:     return 0;

  82: }

Loj 1003–Drunk(拓扑排序)的更多相关文章

  1. Lightoj 1003 - Drunk(拓扑排序)

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

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

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

  3. [LOJ 3101] [Luogu 5332] [JSOI2019]精准预测(2-SAT+拓扑排序+bitset)

    [LOJ 3101] [Luogu 5332] [JSOI2019]精准预测(2-SAT+拓扑排序+bitset) 题面 题面较长,略 分析 首先,发现火星人只有死和活两种状态,考虑2-SAT 建图 ...

  4. 洛谷 P3975 / loj 2102 [TJOI2015] 弦论 题解【后缀自动机】【拓扑排序】

    后缀自动机入门. 题目描述 为了提高智商,ZJY 开始学习弦论. 这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为 \(n\) 的字符串,求出它的第 \ ...

  5. 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】

    组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n​\) 个穴位,穴位的编号为 \(1\sim n​\).有若干条有向的脉络连接着这些穴位. ...

  6. 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)

    洛谷题目传送门 你谷无题解于是来补一发 随便百度题解,发现了不少诸如树剖\(log^3\)LCT\(log^2\)的可怕描述...... 于是来想想怎么利用题目的性质,把复杂度降下来. 首先,每个点的 ...

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

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

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

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

  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. 如何使用mybatis《一》

    mybatis作为ORM轻量级框架一出现就吸引了无数人的眼球,比hibernate要简单且入门较容易,下面开始我的第一个mybatis程序. 一.下载mybatis的包 我们知道任何一个框架都会有其包 ...

  2. Dalvik VM和JVM 的比较

    dx:dx工具用于将.class字节码(bytecode)转换为Android字节码(保存在.dex文件中)这个字节码文件 是给 Android 的 Java 虚拟机(Dalvik虚拟机)运行用的. ...

  3. [模仿][JS]新浪财经7*24直播

    使用新浪财经7*24直播的数据 简单的做一个山寨品 在线地址:[痛苦啊,有GFW,却没有vpn,往heroku上传浪费了好多时间...] http://wangxinsheng.herokuapp.c ...

  4. gulp入坑系列(2)——初试JS代码合并与压缩

    在上一篇里成功安装了gulp到项目中,现在来测试一下gulp的合并与压缩功能 gulp入坑系列(1)--安装gulp(传送门):http://www.cnblogs.com/YuuyaRin/p/61 ...

  5. Mvc项目架构分享之项目扩展

    Mvc项目架构分享之项目扩展 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 5.项目 ...

  6. ASP.NET MVC中将数据从Controller传递到视图

    ASP.NET MVC中将数据从Controller传递到视图方法 1.ViewData ViewData的类型是字典数据,key-value 如:ViewData["Data"] ...

  7. 理解Lucene索引与搜索过程中的核心类

    理解索引过程中的核心类 执行简单索引的时候需要用的类有: IndexWriter.ƒDirectory.ƒAnalyzer.ƒDocument.ƒField 1.IndexWriter IndexWr ...

  8. 基础学习day09--内部类

    一.内部类 1.1.内部类概念 定义在一个类内部中的另一个类,被称为内部类 特点: 可以直接访问,内部类的外部类中的,成员属性和方法(包含-private) 外部类要访问内部类中的成员和方法,需要先建 ...

  9. iOS应用程序开发之应用间的跳转(用着微信等第三方分享登陆)

    简介 配置和实现 判断应用启动方式 一.简介 最实际项目开发中,我们难免会遇到需要从一个应用跳转到另一个应用的情况.比如微信分享,实际就是一种应用间的跳转.但是有时候我们需要实现自己的两个应用间的跳转 ...

  10. 网易新闻iOS版使用的18个开源组件

    转载来自:http://www.jianshu.com/p/8952944f7566  原文最后编辑时间:2015.05.19 网易新闻iOS版在开发过程中曾经使用过的第三方开源类库.组件 1.AFN ...