Repository navigation
Expand file tree
/
Copy path1003.cpp
More file actions
88 lines (77 loc) · 1.91 KB
/
Copy path1003.cpp
File metadata and controls
88 lines (77 loc) · 1.91 KB
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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
#define ft first
#define sd second
#define mp make_pair
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mem(a, b) memset(a, b, sizeof(a))
#define meminf(a) memset(a, 126, sizeof(a))
#define inf 1e11
#define eps 1e-9
#define mod 1000000007
#define NN 100010
// cout << setfill('0') << setw(3) << a << endl;
// cout << fixed << setprecision(20) << a << endl;
vector<int> e[NN];
map<string, int> mpp;
int color[NN];
int fl;
void dfs(int u) {
color[u] = 1;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!color[v])
dfs(v);
else if (color[v] == 1) {
fl = -1;
return;
}
}
color[u] = 2;
return;
}
string s, ss;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 0, tc;
int n, m, i, j, l;
cin >> tc;
while (tc--) {
cin >> m;
int r = 0;
n = 0;
for (i = 0; i < m; i++) {
cin >> s >> ss;
if (mpp.find(s) == mpp.end()) mpp[s] = r++;
if (mpp.find(ss) == mpp.end()) mpp[ss] = r++;
e[mpp[ss]].pb(mpp[s]);
}
mem(color, 0);
fl = 0;
for (i = 0; i < r; i++) {
if (!color[i]) dfs(i);
if (fl == -1) break;
}
if (fl != -1)
printf("Case %d: Yes\n", ++t);
else
printf("Case %d: No\n", ++t);
for (i = 0; i < r; i++) e[i].clear();
mpp.clear();
}
return 0;
}