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

#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. C# 异步下载文件

    在C#当中,利用WebClient这个核心类,可以轻易的打造一个下载器.但是这里想要强调的是,我们用的是异步操作.所谓异步,是相对于同步的概念而言的.比如Web中的Ajax就是基于异步的.它能够提供良 ...

  2. SQL Server 2012 读写分离设置

    SQL Server 2012 读写分离设置 - AlsoIn 时间 2014-07-21 17:38:00  博客园-所有随笔区 原文  http://www.cnblogs.com/also/p/ ...

  3. 如何使用ERStudio 生成comment

    在ER使用中,在生成sql过程中,如何批量生成字段描述,如何批量添加Owner,请看下文: 1.ER生成字段描述 2.ER生成描述添加Owner 使用的ER版本是8.0,英文版本,在操作过程中,有些配 ...

  4. ECMAScript 6十大特性

    ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...

  5. RHEL 6.4 64bit kettle5.01导入xlsx格式的excel时报错

    环境:RHEL 6.4 64bit : kettle5.01:xlsx格式的excel 创建的job,在spoon里面运行都没有问题(Linux和windows) 在windows的命令行运行也没有问 ...

  6. 在Linux系详解Linux bash中的变量

    (大讲台:国内首个it在线教育混合式自适应学习) 统中进行日常运维或者是编写脚本时,变量是再熟悉不过的了,但这些变量都有哪些类型,具体的用法又有哪些差异呢?本文整理分享给大家: 一.bash变量类型: ...

  7. 关于php程序员 解决问题的能力

    转载于 :http://www.tuicool.com/articles/qeUfEf 这个话题老生长谈了,在面试中必然考核的能力中,我个人认为解决问题能力是排第一位的,比学习能力优先级更高.解决问题 ...

  8. MaskedTextBox控件实现输入验证

    Mask属性可以验证用户在文本中输入数据的格式 this.maskedTextBox1.Mask = "000000-00000000-000A";//身份证号码18位 this. ...

  9. mongodb 简单部署方案及实例

    mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb  我这里选用rehl 5.6 ...

  10. python 读写文本文件

    本人最近新学python ,用到文本文件的读取,经过一番研究,从网上查找资料,经过测试,总结了一下读取文本文件的方法. 1.在读取文本文件的时无非有两种方法: a.f=open('filename', ...