# Lightoj1003 drunk

## 拓扑排序判环

Posted by yjjr's blog on March 11, 2019

# Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define ll long long
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e4+6;
#include<map>
#include<queue>
queue<int> que;
map<string,int> mp;
int m,n,du[maxn],cnt,tot,last[maxn],T;
char a[16],b[16];
struct edge{int to,next;}e[maxn<<1];
void insert(int u,int v){e[++cnt]=(edge){v,last[u]};last[u]=cnt;}

int main(){
rep(TT,1,T){
while(!que.empty())que.pop();
cnt=tot=n=0;
mem(last,0);mem(du,0);
mp.clear();
rep(i,1,m){
scanf("%s %s",a,b);
if(mp.find(a)==mp.end())mp[a]=++n;
if(mp.find(b)==mp.end())mp[b]=++n;
insert(mp[a],mp[b]);du[mp[b]]++;
}
tot=n;
rep(i,1,n)if(du[i]==0)que.push(i);
while(!que.empty()){
int now=que.front();que.pop();tot--;
reg(now){
du[e[i].to]--;
if(du[e[i].to]==0)que.push(e[i].to);
}
}
if(!tot)printf("Case %d: Yes\n",TT);else printf("Case %d: No\n",TT);
}
return 0;
}