-
Notifications
You must be signed in to change notification settings - Fork 1
/
1003 - Drunk.cpp
101 lines (67 loc) · 1.59 KB
/
1003 - Drunk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
#define mx 10005
map<string, int> mp;
string a,b;
int ind[mx] = {0};
vector<int> adj[mx];
void topSort(int n)
{
for(int i=1; i<=n; i++){
if(ind[i]==0){
int sz = adj[i].size();
for(int j=0; j<sz; j++){
ind[ adj[i][j] ]--;
}
ind[i] = -1;
i = 0;
}
}
}
int main()
{
//freopen("out.txt","w",stdout);
int tc,n,x,y;
scanf("%d",&tc);
for(int t=1; t<=tc; t++){
int cunt = 0;
scanf("%d",&n);
for(int i=1; i<=n; i++){
cin>>a>>b;
if(mp.find(a) != mp.end()){
x = mp[a];
}
else{
mp[a] = (++cunt);
x = cunt;
}
if(mp.find(b) != mp.end()){
y = mp[b];
}
else{
mp[b] = (++cunt);
y = cunt;
}
adj[x].push_back(y);
ind[y]++;
}
topSort(cunt);
bool ans = true;
for(int i=1; i<=cunt; i++){
if(ind[i]>0){
ans = false;
break;
}
}
printf("Case %d: ",t);
ans == true ? printf("Yes\n") : printf("No\n");
if(t != tc){
mp.clear();
for(int i=1; i<=cunt; i++){
ind[i] = 0;
adj[i].clear();
}
}
}
return 0;
}