-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1003 - Drunk
89 lines (79 loc) · 1.6 KB
/
1003 - Drunk
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
/*
Name: Raihan Chowdhury
International Islamic University Chittagong
Email: [email protected]
Problem: Graph (lightoj 1003)
comment: Topological Sort test by finding cycle
*/
#include <bits/stdc++.h>
using namespace std;
vector<int> v[10005];
int c[100005];
bool Top_sort(int total)
{
int ans = total;
int cnt = 0;
queue<int> q;
for(int i = 1; i <= total; i++)
{
if(c[i] == 0)
{
cnt++;
q.push(i);
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 0; i < v[u].size(); i++)
{
int vx = v[u][i];
c[vx]--;
if(c[vx] == 0)
{
cnt++;
q.push(vx);
}
}
}
if(cnt == ans)
return true;
else
return false;
}
int main()
{
int tes , o = 0;
cin >> tes;
while(tes--)
{
o++;
int q;
cin >> q;
string a;
string b;
int in = 1;
map< string, int> mp;
memset(c, 0, sizeof c);
for(int i = 0; i < q; i++)
{
cin >> a;
cin >> b;
if(mp[a] == 0)
mp[a] = in++;
if(mp[b] == 0)
mp[b] = in++;
v[mp[a]].push_back(mp[b]);
c[mp[b]]++;
}
bool x = Top_sort(in - 1);
if(x == true)
printf("Case %d: Yes\n",o);
else
printf("Case %d: No\n",o);
mp.clear();
for(int i = 1; i <= in; i++)
v[i].clear();
}
}