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 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(拓扑排序)的更多相关文章
- Loj 1003–Drunk(拓扑排序)
1003 - Drunk PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB One of my fr ...
- 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 ...
- LightOJ1003---Drunk(拓扑排序判环)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- HDU2094产生冠军 (拓扑排序)
HDU2094产生冠军 Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛. 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认 ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
随机推荐
- Python数据结构--树遍历算法
''' 遍历是访问树的所有节点的过程,也可以打印它们的值. 因为所有节点都通过边(链接)连接,所以始终从根(头)节点开始. 也就是说,我们不能随机访问树中的一个节点. 这里介绍三种方式来遍历一棵树 - ...
- HDU 2243 考研路茫茫——单词情结
考研路茫茫——单词情结 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...
- JS 回车跳转
window.onload = function () { SeteasyuiTheme(); jQuery(':input:enabled').addClass('enterIndex'); var ...
- [luoguP1360] [USACO07MAR]黄金阵容均衡Gold Balanced L…
传送门 真的骚的一个题,看了半天只会个前缀和+暴力.. 纯考思维.. 良心题解 #include <cstdio> #include <cstring> #include &l ...
- 哀悼改变全站颜色为灰色CSS代码收藏
<style type="text/css"> *{ filter:progid:DXImageTransform.Microsoft.BasicIma ...
- Python基础之 一 集合(set)
集合:是一个无序的,不重复的数据组合.主要作用: 去重(把列表变成集合就自动去重) 关系测试 测试俩组数据的交集,差集,并集等关系 关系测试共有7种,如下: 名称 方法名 简写符号 解释交集 s.in ...
- HDU 1669 二分图多重匹配+二分
Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- Wannafly挑战赛4
A(枚举) =w= B(枚举) 分析: 枚举每一位,考虑每位贡献,就是相当于在一段区间内找有多少1在奇数位上,有多少个1在偶数位上,维护一下各自前缀和就行了 时间复杂度O(32n) C(签到) D(d ...
- Java的循环结构
以下内容引用自http://wiki.jikexueyuan.com/project/java/loop-control.html: 可能存在一种情况,当需要执行的代码块数次,通常被称为一个循环.Ja ...
- RHEL 启动系统及故障排除
一:Linux的启动过程: 开机加电自检->MBR引导(boot loader占446字节,分区列表64字节,magic占2字节)-->grub菜单(MBR是grub的第一个字段,第二个字 ...