forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
224 lines (195 loc) · 5.04 KB
/
H.cpp
File metadata and controls
224 lines (195 loc) · 5.04 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
int GI(int& x) {
return scanf("%lld", &x);
}
template<class T> struct splnode {
typedef splnode<T> node_t;
splnode() : P(NULL), flip(0), pp(NULL) {
C[0] = C[1] = NULL;
fix();
}
node_t* P;
node_t* C[2];
int flip;
node_t* pp;
/* Fix the parent pointers of our children. Additionally if we have any
* extra data we're tracking (e.g. sum of subtree elements) now is the time to
* update them (all of the children will already be up to date). */
void fix() {
if(C[0]) C[0]->P = this;
if(C[1]) C[1]->P = this;
}
/* Push the flip bit down the tree. */
void push_flip() {
if(!flip) return;
flip = 0;
swap(C[0], C[1]);
if(C[0]) C[0]->flip ^= 1;
if(C[1]) C[1]->flip ^= 1;
}
/* Return the direction of this relative its parent. */
int up() {
return !P ? -1 : (P->C[0] == this ? 0 : 1);
}
/* Simple zig step in the 'c' direction when we've reached the root. */
void zig(int c) {
node_t* X = C[c];
if(X->P = P) P->C[up()] = X;
C[c] = X->C[1 - c];
X->C[1 - c] = this;
fix(); X->fix();
if(P) P->fix();
swap(pp, X->pp);
}
/* Zig zig in the 'c' direction both times. */
void zigzig(int c) {
node_t* X = C[c];
node_t* Y = X->C[c];
if(Y->P = P) P->C[up()] = Y;
C[c] = X->C[1 - c];
X->C[c] = Y->C[1 - c];
Y->C[1 - c] = X;
X->C[1 - c] = this;
fix(); X->fix(); Y->fix();
if(P) P->fix();
swap(pp, Y->pp);
}
/* Zig zag first in the 'c' direction then in the '1-c' direciton. */
void zigzag(int c) {
node_t* X = C[c];
node_t* Y = X->C[1 - c];
if(Y->P = P) P->C[up()] = Y;
C[c] = Y->C[1 - c];
X->C[1 - c] = Y->C[c];
Y->C[1 - c] = this;
Y->C[c] = X;
fix(); X->fix(); Y->fix();
if(P) P->fix();
swap(pp, Y->pp);
}
/* Splay this up to the root. Always finishes without flip set. */
node_t* splay() {
for(push_flip(); P; ) {
/* Reorganize flip bits so we can rotate as normal. */
if(P->P) P->P->push_flip();
P->push_flip();
push_flip();
int c1 = up();
int c2 = P->up();
if(c2 == -1) {
P->zig(c1);
} else if(c1 == c2) {
P->P->zigzig(c1);
} else {
P->P->zigzag(c2);
}
}
return this;
}
/* Return the max element of the subtree rooted at this. */
node_t* last() {
push_flip();
return C[1] ? C[1]->last() : splay();
}
/* Return the min element of the subtree rooted at this. */
node_t* first() {
push_flip();
return C[0] ? C[0]->first() : splay();
}
};
template<class T>
struct linkcut {
typedef splnode<T> node_t;
linkcut(int N) : node(N) {
}
void link(int u, int v) {
make_root(v);
node[v].pp = &node[u];
}
void cut(int u, int v) {
make_root(u);
node[v].splay();
if(node[v].pp) {
node[v].pp = NULL;
} else {
node[v].C[0]->P = NULL;
node[v].C[0] = NULL;
node[v].fix();
}
}
bool connected(int u, int v) {
node_t* nu = access(u)->first();
node_t* nv = access(v)->first();
return nu == nv;
}
/* Move u to root of represented tree. */
void make_root(int u) {
access(u);
node[u].splay();
if(node[u].C[0]) {
node[u].C[0]->P = NULL;
node[u].C[0]->flip ^= 1;
node[u].C[0]->pp = &node[u];
node[u].C[0] = NULL;
node[u].fix();
}
}
/* Move u to root aux tree. Return the root of the root aux tree. */
splnode<T>* access(int u) {
node_t* x,* pp;
for(x = node[u].splay(); x->pp; x = pp) {
pp = x->pp->splay();
x->pp = NULL;
if(pp->C[1]) {
pp->C[1]->P = NULL;
pp->C[1]->pp = pp;
}
pp->C[1] = x;
pp->fix();
}
return x;
}
vector<node_t> node;
};
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0);
int n; cin >> n;
linkcut<int> lc(n);
while (true) {
char typ; cin >> typ;
if (typ == 'C') {
int u, v; cin >> u >> v;
--u; --v;
lc.link(u, v);
}
else if (typ == 'T') {
int u, v; cin >> u >> v;
--u; --v;
if (lc.connected(u, v)) puts("YES");
else puts("NO");
fflush(stdout);
}
else if (typ == 'D') {
int u, v; cin >> u >> v;
--u; --v;
lc.cut(u, v);
}
else { // E
break;
}
}
}