lightoj 1003
有向图拓扑排序,判段是否存在。
#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的更多相关文章
- 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 ...
- LightOJ - 1003 Drunk
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- lightoj 1381 - Scientific Experiment dp
1381 - Scientific Experiment Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lightoj.com/vo ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- 区间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 ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
随机推荐
- NUnit单元测试
单元测试对程序员来说是非常重要的一门技术,但是在实际编程中却往往被程序员所忽视.微软的VS开发工具为我们提供了强大的单元测试环境,在VS当中可以直接对类库项目进行测试,极大的方便了程序员的自我纠错能力 ...
- WPF异步调用WCF服务
wpf调用wcf时,第一次访问总耗时到达几秒,影响界面的用户体验,因此在wpf加载界面和加载数据时采用异步加载,即异步访问wcf服务, 由于是否采用异步加载和服务端无关,仅仅由客户端自己根据需要来选择 ...
- matlab2014在mac Yosemite下出现java空指针情况
恢复方法为 使用xcode打开 /System/Library/CoreServices/SystemVersion.plist 将 ProductVersion 下的10.10或10.10.1改为1 ...
- python--threading多线程总结
threading用于提供线程相关的操作,线程是应用程序中工作的最小单元.python当前版本的多线程库没有实现优先级.线程组,线程也不能被停止.暂停.恢复.中断. threading模块提供的类: ...
- hadoop基本命令1
(大讲台——国内首个it在线混合式自适应学习平台,轻量级的高薪就业和技能提升解决方案) 1.列出所有Hadoop Shell支持的命令$ bin/hadoop fs -help2.显示关于某个命令的详 ...
- Java EJX
EJX http://www.docin.com/p-121548732.html
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...
- [转载]MongoDB开发学习(2)索引的基本操作
索引能够极大的提高查询的效率.在数据库中简历索引必不可少. 在MongoDB中可以很轻松的创建索引. 默认索引_id_ 开启MongoDB服务器,创建数据库cnblogs,创建集合Users .(关于 ...
- PHP漏洞全解(六)-跨网站请求伪造
本文主要介绍针对PHP网站的跨网站请求伪造.在CSRF所有攻击方式中包含攻击者伪造一个看起来是其他用户发起的HTTP 请求,事实上,跟踪一个用户发送的HTTP请求才是攻击者的目的. CSRF(Cros ...
- ThinkPHP中视图模型详解.
很多TP的新手对于模型中的视图模型不甚了解,官方虽然有详细手册,但是对于初学者来说还是比较难以理解! 先简单说一下视图模型所能实现的功能,基本就是主表与副表之间各个字段的关联问题,实现多表关联查询,相 ...