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 ...
随机推荐
- UE4之Slate:App默认窗口的创建流程
UE4版本:4.24.3源码编译 Windows10 + VS2019开发环境 在先前分享的基础上,现在来梳理下App启动时默认窗口的创建流程,以及相关的类.对象之间的抽象层级: 纯C++工程配置 S ...
- HDFS05 NameNode和SecondaryNameNode
NameNode和SecondaryNameNode(了解) 目录 NameNode和SecondaryNameNode(了解) NN 和 2NN 工作机制 NameNode工作机制 Secondar ...
- 日常Java 2021/10/28
Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...
- R语言学习记录(一)
(R基础) 对象:什么是对象呢,其实就是一个名称而已,在R中存储的数据 就是一个R对象 a <- 1 ###其中'<-'表示的是一个赋值符号 这句话表示的是,将1赋值给a b <- ...
- Spark(九)【RDD的分区和自定义Partitioner】
目录 spark的分区 一. Hash分区 二. Ranger分区 三. 自定义Partitioner 案例 spark的分区 Spark目前支持Hash分区和Range分区,用户也可以自定义分区 ...
- 容器之分类与各种测试(四)——unordered-multimap
unordered-multiset与unordered-multimap的区别和multiset与multimap的区别基本相同,所以在定义和插入时需要注意 key-value 的类型. 例程 #i ...
- myBatis批量添加实例
<!-- 批量添加中转地数据 --> <insert id="addBatch" parameterType="com.isoftstone. ...
- Dubbo服务暴露延迟
Dubbo 2.6.5 版本以后,如果我们的服务启动过程需要warmup事件,就可以使用delay进行服务延迟暴露.只需在服务提供者的<dubbo:service/>标签中添加delay属 ...
- js 时间戳转换为年月日时分秒的格式
<script type="text/javascript"> var strDate = ''; $(function(){ // 获取时间戳 var nowDate ...
- 利用ajax,js以及正则表达式来验证表单递交
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" ...