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

#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. Entity Framework 插入数据 解决主键非自增问题

    http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐,两个痛苦. ...

  2. 【转】怎样将DataGridView中绑定的表的列名改成中文

    在DataGridView设置数据源绑定后,设置DataGridView的属性HeaderText就可以了.代码参考: dataGridView.Columns[filedName].HeaderTe ...

  3. HttpWebRequest中的KeepAlive

    一直不是非常理解.NET中HttpWebRequest的KeepAlive属性有何用处,看了这篇文章就清楚了! http://www.cnblogs.com/lwzz/archive/2011/08/ ...

  4. laravel的多态关联--morphTo和morphMany

    首先,文档里面讲述的不是特别详细,详细寻找查询流程没有过多介绍,只是介绍如何去定义,直接使用,导致很多该明白的东西,没有说明,下面详细看看这个多态关联 是怎么定义,使用,详细查询的. 先看文档介绍 多 ...

  5. 在jsp中运用ajax实现同一界面不跳转处理事件

    目前,编写应用程序时有两种基本的选择: 桌面应用程序 Web应用程序 它们有什么区别呢?桌面应用程序一般很快(就在您的计算机上运行,不用等待互联网连接),具有漂亮的用户界面(通常和操作系统有关)和非凡 ...

  6. PL/SQL — 函数

    函数通常用于返回特定的数据.其实质是一个有名字的PL/SQL块,作为一个schema对象存储于数据库,可以被反复执行.函数通常被作为一个表达式来调用或存储过程的一个参数,具有返回值.   一.建立函数 ...

  7. IQKeyboredManager使用

    这个库是一个单例,它一旦生效,全项目任何界面都有效.让它生效的代码可以写在任意位置,我写在AppDelegate里. 1 2 3 4 5 6 7 8 9 10 - (BOOL)application: ...

  8. iOS8上放大缩小的动画

    CGAffineTransformMakeScale这个方法我们以前经常使用,但是在IOS8上出现问题了 [UIView animateWithDuration:0.3 animations:^{ b ...

  9. python 程序列表

    用 python  通过读取注册表来获取机器安装的程序列表,包括,软件名称,版本号,安装日期等 # -*- coding: UTF8 -*-import _winregimport osimport ...

  10. JavaScript中===与==的区别

    参考网址:http://zhidao.baidu.com/link?url=RbWnRUHKPBTakwm2-iRfEDicrh4ZSAAeVfq2WvBkvfeLPkU-ZSqZ4chV8LWkCk ...