-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
53 lines (44 loc) · 948 Bytes
/
main.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
#include <bits/stdc++.h>
using namespace std;
int tab[300005];
int weight[300005];
int find(int a){
if(a != tab[a])
tab[a] = find(tab[a]);
return tab[a];
}
bool find(int a, int b){
return find(a) == find(b);
}
void uni(int a, int b){
if(!find(a, b)){
a = find(a), b = find(b);
if(weight[a] < weight[b])
tab[a] = b;
else if(weight[a] > weight[b])
tab[b] = a;
else
tab[b] = a, weight[a] += weight[b];
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int v, e, t; cin >> v >> e >> t;
for(int i = 0; i <= v; i++)
tab[i] = i;
int a, b;
while(e--){
cin >> a >> b;
uni(a, b);
}
bool q;
while(t--){
cin >> q >> a >> b;
if(q) //union
uni(a, b);
else //find
cout << (find(a, b) ? "TAK\n" : "NIE\n");
}
return 0;
}