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

#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. 使用.htaccess进行浏览器图片文件缓存

    对于图片类网站,每次打开页面都要重新下载图片,慢不说,还非常浪费流量.这时就需要用到缓存,强制浏览器缓存图片文件 缓存文件,提问网站访问数度,减少流量消耗,现提供2中缓存代码 打开.htaccess文 ...

  2. Python 基础篇:字典、集合、文件操作

    字典 字典一种key - value 的数据类型 1. 语法: info = { 'stu1101': "TengLan Wu", 'stu1102': "LongZe ...

  3. sscanf用法简析

    1. 常见用法. char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n" ...

  4. Git权威指南 读笔(1)

    第四章 Git初始化: 设置Git当前用户和邮件地址: $ git config --global user.name $ git config --global user.email 设置Git命令 ...

  5. windows store app Lifecycle

    1.Activated 2.Suspended 3.Resumed 4.Terminated 对应的 js代码: (function () { "use strict"; WinJ ...

  6. Automotive Security的一些资料和心得(3):Vehicular Security技术

    1. Overview 1.1. Secure Hardware Extension (SHE) 基本结构:ECU里面有一块单独的Secure Zone.Secure Zone里面是SHE模块.SHE ...

  7. c++ 联合体

    联合体分配的内存大小是成员变量中最大变量的大小 联合体的成员变量共享内存 小段模式(X86就是) 低位数据存在低地址单元 大端模式                     高位字节存在低地址单元

  8. js二级下拉菜单

    看似简单的一个菜单,确需要不少的知识点 1. getByClass getElementsByClassName 已经有大部分现代浏览器支持了,只有ie6,ie7,ie8是不支持的.所以对ie6,7, ...

  9. OpenVPN下载、安装、配置及使用详解

    OpenVPN下载.安装.配置及使用详解   OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可 ...

  10. 严重: The web application [] registered the JDBC driver 错误

    近日发现启动tomcat的时候报如下警告: -- :: org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 严重: The ...