有向图拓扑排序,判段是否存在。

#include<map>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 20010;
struct Edge{
int to, next;
};
Edge edge[MAXN >> 1];
int head[MAXN], ind[MAXN];
void addEdge(int u, int v, int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
int main(){
int t, m, CASE(0);
string str1, str2;
//freopen("in.cpp", "r", stdin);
scanf("%d", &t);
while(t--){
int NUM(0), cnt(0);
map<string, int>mp;
memset(ind, 0, sizeof ind);
memset(head, -1, sizeof head);
scanf("%d", &m);
for(int i = 0;i < m;i ++){
int u, v;
cin >> str1 >> str2;
map<string, int>::iterator it = mp.find(str1);
if(it == mp.end()) mp.insert(pair<string, int>(str1, ++NUM)), u = NUM;
else u = it->second;
it = mp.find(str2);
if(it == mp.end()) mp.insert(pair<string, int>(str2, ++NUM)), v = NUM;
else v = it->second;
ind[u]++;
addEdge(v, u, i+1);
}
while(cnt < NUM){
int pos = 0;
for(int i = 1;i <= NUM;i ++){
if(ind[i] == 0){
cnt++;
ind[i]--;
pos = i;
break;
}
}
if(!pos) break;
for(int i = head[pos]; ~i; i = edge[i].next){
int v = edge[i].to;
ind[v]--;
}
}
if(cnt == NUM) printf("Case %d: Yes\n", ++CASE);
else printf("Case %d: No\n", ++CASE);
}
return 0;
}

lightoj 1003的更多相关文章

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

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

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

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

  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. lightoj 1381 - Scientific Experiment dp

    1381 - Scientific Experiment Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lightoj.com/vo ...

  5. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  6. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  7. Bestcoder#5 1003

    Bestcoder#5 1003 Poor RukawTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 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 ...

  9. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

随机推荐

  1. MVC文件上传-使用jQuery.FileUpload和Backload组件实现文件上传

    本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: 处理文件上传的服务端组件Backload 用于处理文件上传的服务端组件 ...

  2. winform水晶报表编译错误,未能加载文件或程序集"file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framewor "

    未能加载文件或程序集“file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Commo ...

  3. yii2 model常用验证规则

    //字段必填[['username'],'required','message'=>'{attribute}不能为空!'][['username','password'], 'required' ...

  4. platform平台设备驱动简化示例代码

    driver.c: #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h& ...

  5. find grep

    grep grep -rn "hello,world!" * #递归查找当前目录下所有包含hello,world的文件 grep -C number pattern files : ...

  6. WPF中的一些常用类型转换

    1.string和Color的转换: //string转Color (Color)ColorConverter.ConvertFromString((string)str); //Color转stri ...

  7. Windows操作系统常用快捷键

    复制:ctrl+c       剪切:ctrl+x      粘贴:ctrl+v       全选:ctrl+a         撤消:ctrl+z      保存:ctrl+s 运行:win+r   ...

  8. STL set_difference set_intersection set_union 操作

    以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...

  9. 认识变量------JAVA

    声明变量: variables must have a type variables must have a name int count // type int //count name 变量就是杯 ...

  10. ZOJ 3609 Modular Inverse

    点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...