LightOJ - 1003 Drunk
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 havingb. 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 |
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 #include<vector>
7 #include<string.h>
8 #include<math.h>
9 #include<map>
10 #include<vector>
11 char str1[20];
12 char str2[20];
13 int cnt[200005];
14 int top(int ans,int nn,int k);
15 using namespace std;
16 vector<int>vec[20005];
17 int main(void)
18 {
19 int i,j,k,p,q;
20 int s;
21 scanf("%d",&k);
22 for(s=1; s<=k; s++)
23 {
24 map<string,int>my;
25 memset(cnt,0,sizeof(cnt));
26 scanf("%d",&p);
27 int ans=1;
28 for(i=0; i<20004; i++)
29 vec[i].clear();
30 for(i=0; i<p; i++)
31 {
32 scanf("%s %s",str1,str2);
33 if(my[str1]==0)
34 {
35 my[str1]=ans++;
36 }
37 if(my[str2]==0)
38 {
39 my[str2]=ans++;
40 }
41 vec[my[str1]].push_back(my[str2]);
42 cnt[my[str2]]++;
43 }
44 int ss=top(ans-1,0,ans-1);
45 printf("Case %d: ",s);
46 if(ss)printf("Yes\n");
47 else printf("No\n");
48 }
49 return 0;
50 }
51 int top(int ans,int nn,int k)
52 {
53 int i,j;
54 queue<int>que;
55 for(i=1; i<=ans; i++)
56 if(cnt[i]==0)
57 {
58 nn++;
59 que.push(i);
60 }
61 while(!que.empty())
62 {
63 int y=que.front();
64 que.pop();
65 for(i=0; i<vec[y].size(); i++)
66 {
67 cnt[vec[y][i]]--;
68 if(cnt[vec[y][i]]==0)
69 {
70 nn++;
71 que.push(vec[y][i]);
72 }
73 }
74 }
75 if(nn==k)return 1;
76 else return 0;
77 }
LightOJ - 1003 Drunk的更多相关文章
- Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...
- Lightoj 1003 - Drunk(拓扑排序)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- Loj 1003–Drunk(拓扑排序)
1003 - Drunk PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB One of my fr ...
- lightoj 1003
有向图拓扑排序,判段是否存在. #include<map> #include<cstdio> #include<string> #include<cstrin ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- lightoj 1381 - Scientific Experiment dp
1381 - Scientific Experiment Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lightoj.com/vo ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- Bestcoder#5 1003
Bestcoder#5 1003 Poor RukawTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- 简单mvc框架核心笔记
简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...
- Scrapy-Splash的安装和使用
Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Docker,安装之后,会启动一 ...
- Redis篇:单线程I/O模型
关注公众号,一起交流,微信搜一搜: 潜行前行 redis 单线程 I/O 多路复用模型 纯内存访问,所有数据都在内存中,所有的运算都是内存级别的运算,内存响应时间的时间为纳秒级别.因此 redis 进 ...
- GISer如何突破二次开发瓶颈
年初时写的<一个GISer的使命>那篇文章中,提出了GISer的技术提升路径可以分为四个大的阶段: 阶段一,能使用商业GIS软件去解决问题. 阶段二,能使用开源GIS软件去解决问题. 阶段 ...
- keras房价预测数据集boston_housing.npz文件下载
github地址: https://github.com/yuxiwang66/boston_housing 码云地址: https://gitee.com/polarisslk/boston_hou ...
- 到底什么是自动化优先思维?与RPA有什么关系?
基于RPA的自动化优先,正在成为广大组织的主流管理思维 到底什么是自动化优先思维?与RPA有什么关系? 如何用RPA简单快速的打造一个自动化优先的组织? 文/王吉伟 在IT运维项目中,组织经常会遇到先 ...
- Js数组内对象去重
let person = [ {id: 0, name: "小明"}, {id: 1, name: "小张"}, {id: 2, name: "小李& ...
- LINUX 安装增强 前置安装文件
yum install kernel yum install kernel-devel yum install gcc yum install make
- 网络通信引擎ICE的使用
ICE是一种网络通信引擎,在javaWeb的开发中可以用于解决局域网内部服务器端与客户端之间的网络通信问题.即可以在 1.在服务器和客户端都安装好ICE 2.服务器端(java)在java项目中引入I ...
- HTTPS及流程简析
[序] 在我们在浏览某些网站的时候,有时候浏览器提示需要安装根证书,可是为什么浏览器会提示呢?估计一部分人想也没想就直接安装了,不求甚解不好吗? 那么什么是根证书呢?在大概的囫囵吞枣式的百度之后知道了 ...