-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10158 War.cpp
104 lines (93 loc) · 2 KB
/
10158 War.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define pb push_back
using namespace std;
vector <int> pai, hated;
int find(int a){
if(a==pai[a]) return a;
return pai[a] = find(pai[a]);
}
void join(int a, int b){
a = find(a);
b = find(b);
if(a==b) return;
pai[b] = a;
}
bool areEnemies(int a, int b){
int ax = find(a), bx = find(b), ae, be;
if(hated[ax]==-1 || hated[bx]==-1) return false;
if(find(hated[ax])==bx || find(hated[bx])==ax) return true;
else return false;
}
bool areFriends(int a, int b){
if(find(a)==find(b)) return true;
else return false;
}
void setFriends(int a, int b){
int ax = find(a), bx = find(b), ae, be;
if(!areEnemies(ax, bx)){
join(ax, bx);
if(hated[ax]==-1 && hated[bx]==-1) return;
else if(hated[bx]==-1) return;
else if(hated[ax]==-1){
hated[ax] = find(hated[bx]);
hated[hated[ax]] = ax;
}
else{
ae = find(hated[ax]);
be = find(hated[bx]);
join(ae, be);
}
}else printf("-1\n");
}
void setEnemies(int a, int b){
int ax, bx, ae, be;
if(!areFriends(a, b)){
ax = find(a);
be = find(b);
if(hated[ax]==-1 && hated[be]==-1){
hated[ax] = be;
hated[be] = ax;
}else if(hated[ax]==-1){
bx = find(hated[be]);
join(ax, bx);
hated[ax] = be;
hated[be] = ax;
}else if(hated[be]==-1){
ae = find(hated[ax]);
join(ae, be);
hated[be] = ax;
hated[ax] = ae;
}
else{
ae = find(hated[ax]);
bx = find(hated[be]);
join(ax, bx);
join(ae, be);
}
}
}
int main(){
int n, op, a, b;
scanf("%d", &n);
pai.resize(n);
for(int i=0;i<n;i++){
hated.pb(-1);
pai[i]=i;
}
while(scanf("%d %d %d", &op, &a, &b), op!=0){
if(op==1) setFriends(a, b);
else if(op==2) setEnemies(a, b);
else if(op==3){
if(areFriends(a, b)) printf("1\n");
else printf("0\n");
}
else{
if(areEnemies(a, b)) printf("1\n");
else printf("0\n");
}
}
return 0;
}