-
Notifications
You must be signed in to change notification settings - Fork 34
/
1003-Drunk.cpp
112 lines (96 loc) · 1.54 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
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#define white 0
#define gray 1
#define black 2
using namespace std;
vector < vector <string> > a;
map <string, int> hash;
vector <int> visi;
int ok;
int count;
int wow(string x)
{
int t;
int k;
string temp;
t = hash[x];
visi[t] = gray;
for (unsigned int i = 0; i < a[t].size(); i++) {
temp = a[t][i];
if(visi[hash[temp]] == gray) {
ok = 0;
return 0;
} else {
wow(temp);
}
}
visi[t] = black;
}
int main()
{
int t;
int m;
int yes;
int tot;
int c;
c = 1;
tot = 0;
string x;
string y;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> m;
vector <string> backup;
vector < vector <string> > aa(10005);
map <string, int> has;
swap(aa, a);
swap(has, hash);
count = -1;
ok = 1;
for (int j = 0; j < m; j++) {
cin >> x;
cin >> y;
if(hash.count(x) == 0) {
count++;
hash[x] = count;
backup.push_back(x);
tot++;
} else {
count = hash[x];
}
yes = 1;
for (unsigned int g = 0; g < a[count].size(); g++) {
if(a[count][g] == y) {
yes = 0;
}
}
if(yes) {
a[count].push_back(y);
}
if(hash.count(y) == 0) {
count++;
hash[y] = count;
backup.push_back(y);
tot++;
}
}
vector <int> yo(tot+5, 0);
swap(visi, yo);
for (unsigned int j = 0; j < backup.size(); j++) {
if(visi[j] == white) {
wow(backup[j]);
}
if(ok == 0) {
break;
}
}
if(ok) {
cout << "Case "<<i<<": Yes\n";
} else {
cout << "Case "<<i<<": No\n";
}
}
}