-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10158.cpp
138 lines (129 loc) · 2.77 KB
/
10158.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <cstdio>
#include <iostream>
#include <vector>
#define foi(i, n, k) for (int i = n; i < k; ++i)
#define vi std::vector<int>
using namespace std;
class UnionFind {
private:
public:
vi p,rank;
UnionFind(int N) {
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i) {
if(i==-1)
return -1;
return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if(i==-1||j==-1)
return ;
if (!isSameSet( i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y])
p[y] = x;
else {
p[x]=y;
if (rank[x] == rank[y])
rank[y]++;
}
}
}
};
int main()
{
int n,t=0;
scanf("%d",&n );
UnionFind fri(n);
vi ene(n,-1);
int a,b,c;
while( scanf("%d%d%d",&a,&b,&c )!=EOF && (a!=0||b!=0||c!=0) )
{
if( a==1 )
{
int x=fri.findSet(b);
int y=fri.findSet(c);
if( fri.findSet(ene[x])==y )
{
// cout<<++t<<" "<<a<<" "<<b<<" "<<c<<endl;
printf("-1\n" );
}
else{
// cout<<"--- "<<b<<" "<<c<<endl;
fri.unionSet(b,c);
fri.unionSet(c,b);
fri.unionSet(ene[b],ene[c]);
fri.unionSet(ene[c],ene[b]);
int papafin=fri.findSet(b);
int papaene=-1;
if(ene[b]!=-1)
papaene=fri.findSet(b);
else if(ene[c]!=-1)
papaene=fri.findSet(c);
ene[papafin]=papaene;
if(papaene!=-1)
ene[papaene]=papafin;
}
}
else if( a==2 )
{
if(fri.isSameSet(b,c)){
// cout<<++t<<" "<<a<<" "<<b<<" "<<c<<endl;
printf("-1\n" );
}
else
{
int x=fri.findSet(b);
int y=fri.findSet(c);
if(ene[x]!=-1){
fri.unionSet(ene[x],y);
fri.unionSet(y,ene[x]);
}
if(ene[y]!=-1){
fri.unionSet(ene[y],x);
fri.unionSet(x,ene[y]);
}
x=fri.findSet(b);
y=fri.findSet(c);
ene[x]=y;
ene[y]=x;
}
}
else if( a==3 )
{
// cout<<++t<<" "<<a<<" "<<b<<" "<<c<<endl;
printf("%d\n",fri.isSameSet(b,c) );
}
else if( a==4 )
{
// cout<<++t<<" "<<a<<" "<<b<<" "<<c<<endl;
int x=fri.findSet(b);
int y=fri.findSet(c);
// cout<<"-- "<<x<<" "<<y<<endl;
// cout<<fri.findSet(ene[x])<<" "<<y<<endl;
int e=-1;
if(x!=-1)
e=ene[x];
if(fri.isSameSet(x,y))
printf("0\n");
else
printf("%d\n",fri.findSet(e)==y );
}
/*
foi( i , 0 , n )
{
cout<<fri.findSet(i)<<" ";
}
cout<<endl;
foi( i , 0 , n )
{
cout<<ene[i]<<" ";
}
cout<<endl;*/
}
return 0;
}