-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Mahmoud_and_a_Dictionary.cpp
More file actions
109 lines (86 loc) · 2.26 KB
/
D_Mahmoud_and_a_Dictionary.cpp
File metadata and controls
109 lines (86 loc) · 2.26 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
class DSU {
public:
vector<int> par, sz;
DSU(int n){
par.resize(n + 1);
iota(par.begin(), par.end(), 0);
sz.resize(n + 1, 1);
}
int find(int x){
return x == par[x] ? x : par[x] = find(par[x]);
}
bool merge(int u, int v) {
int p1 = find(u), p2 = find(v);
if (p1 == p2) return 0;
if (sz[p1] < sz[p2]) swap(p1, p2);
par[p2] = p1;
sz[p1] += sz[p2];
return 1;
}
};
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<string> v(n);
for(int i=0;i<n;i++) cin >> v[i];
sort(v.begin(), v.end());
DSU ds(2 * n + 1);
vector<int> ans(m);
auto checkenemy = [&](int id1, int id2) {
return ds.find(id1) == ds.find(id2 + n);
};
auto checkfriends = [&](int id1, int id2) {
return ds.find(id1) == ds.find(id2);
};
for(int i=0;i<m;i++) {
int type;
string s, t;
cin >> type >> s >> t;
int id1 = lower_bound(v.begin(), v.end(), s) - v.begin();
int id2 = lower_bound(v.begin(), v.end(), t) - v.begin();
if(type == 1) {
if(!checkenemy(id1, id2)) {
ds.merge(id1, id2);
ds.merge(id1 + n, id2 + n);
ans[i] = 1;
}
}
else {
if(!checkfriends(id1, id2)) {
ds.merge(id1, id2 + n);
ds.merge(id1 + n, id2);
ans[i] = 1;
}
}
}
vector<int> ansq(q);
for(int i=0;i<q;i++) {
string s, t;
cin >> s >> t;
int id1 = lower_bound(v.begin(), v.end(), s) - v.begin();
int id2 = lower_bound(v.begin(), v.end(), t) - v.begin();
if(checkfriends(id1, id2)) ansq[i] = 1;
else if(checkenemy(id1, id2)) ansq[i] = 2;
else ansq[i] = 3;
}
for(int i=0;i<m;i++) {
cout << (ans[i] ? "YES\n" : "NO\n");
}
for(int i=0;i<q;i++) {
cout << ansq[i] << "\n";
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// int _;
// cin >> _;
// while (_-->0) {
solve();
// }
return 0;
}