-
Notifications
You must be signed in to change notification settings - Fork 0
/
L04-4.cpp
61 lines (47 loc) · 1008 Bytes
/
L04-4.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
#include <bits/stdc++.h>
using namespace std;
int N, Q, prn[200005];
int find_prn(int x){
if(prn[x] == x) return x;
return prn[x] = find_prn(prn[x]);
}
void gabung(int x, int y){
int pX = find_prn(x),
pY = find_prn(y);
if(pX == pY) return;
prn[pX] = pY;
}
bool isFriend(int x, int y){
return (find_prn(x) == find_prn(y));
}
bool isEnemy(int x, int y){
return ((find_prn(x) == find_prn(y+N)) || (find_prn(x+N) == find_prn(y)));
}
void setEnemy(int x, int y){
if(isFriend(x, y)) cout << -1 << endl;
else{
gabung(x, y+N);
gabung(x+N, y);
cout << 1 << endl;
}
}
void setFriend(int x, int y){
if(isEnemy(x, y)) cout << -1 << endl;
else{
gabung(x, y);
gabung(x+N, y+N);
cout << 1 << endl;
}
}
int main(){
for(int i=0;i<200005;i++) prn[i] = i;
cin >> N >> Q;
while(Q--){
int id, x, y;
cin >> id >> x >> y;
if(id == 1) setFriend(x, y);
else if(id == 2) setEnemy(x, y);
else if(id == 3) cout << isFriend(x, y) << endl;
else cout << isEnemy(x, y) << endl;
}
}