forked from 3ximus/link-cut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link-cut-tree.c
361 lines (298 loc) · 8.01 KB
/
link-cut-tree.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define NOT_FLIPED 0
#define FLIPED 1
/* ################################################# */
/* ################### NODE ######################## */
/* ################################################# */
typedef struct node_str Node;
struct node_str {
Node *left;
Node *right;
Node *parent;
Node *path_parent; /* We can in the end take this off (or not). Leave it for now, for simplicity. Here there is a tradeoff space/time. */
char is_fliped;
};
Node *make_node() {
Node *res = calloc(sizeof(Node), 1); /* Calloc makes everything NULL */
return res;
}
Node* parent(Node* node) {
return node ? node->parent : NULL;
}
Node* grand_parent(Node* node) {
return parent(parent(node));
}
/* root of its aux tree */
int is_root(Node *node) {
return (node->parent != NULL);
}
int is_left_child(Node* node) {
return (node->parent->left == node);
}
int is_right_child(Node* node) {
return (node->parent->right == node);
}
void unflip(Node *v) {
/* Only something to do if we are fliped */
if(v->is_fliped) {
/* Swap them in memory*/
Node *aux = v->right;
v->right = v->left;
v->left = aux;
/* Make us not fliped and 'flip' the bits of the childs */
v->is_fliped = NOT_FLIPED;
if(v->left) {
v->left->is_fliped = !(v->left->is_fliped);
}
if(v->right) {
v->right->is_fliped = !(v->right->is_fliped);
}
}
}
/* This, where x a y can have subtrees as well
z z
\ /
y OR y
\ \
x x
*/
void rotate_left(Node* y) {
Node *x = y->right;
Node *z = y->parent;
if (z == NULL) {
/* skip */
} else if (is_left_child(y)) {
z->left = x;
} else if (is_right_child(y)) {
z->right = x;
}
y->right = x->left;
x->left = y;
x->parent = z;
y->parent = x;
if (y->right == NULL) {
return;
} else {
y->right->parent = y;
}
}
void rotate_right(Node* y) {
Node *x = y->left;
Node *z = y->parent;
if (z == NULL) {
/* skip */
} else if (is_left_child(y)) {
z->left = x;
} else if (is_right_child(y)) {
z->right = x;
}
y->left = x->right;
x->right = y;
x->parent = z;
y->parent = x;
if (y->left == NULL) {
return;
} else {
y->left->parent = y;
}
}
void splay(Node* x) {
Node *p, *pp;
while (1) {
p = x->parent; /* Parent */
if(p == NULL) {
break;
}
pp = grand_parent(x); /* Grand-parent */
/* Unflip if we have any node fliped (from the grandparent to the child) */
if (pp) {
unflip(pp);
}
unflip(p);
unflip(x);
/* Propagate path_parent */
if (pp == NULL) { /* p is root */
x->path_parent = p->path_parent;
p->path_parent = NULL;
} else if (pp->parent == NULL) { /* pp is root */
x->path_parent = pp->path_parent;
pp->path_parent = NULL;
}
if (is_left_child(x)) {
if (pp == NULL) { /* Zig */
rotate_right(p);
return;
} else if (is_left_child(p)) { /* Zig-Zig -> x is left of p(x) and p(x) is left of g(x) */
rotate_right(grand_parent(x));
rotate_right(parent(x));
} else { /* Zig-Zag -> x is left of p(x) and p(x) is right of g(x) */
rotate_right(parent(x));
rotate_left(parent(x));
}
} else { /* is_right_child(x) */
if (pp == NULL) { /* Zig */
rotate_left(p);
return;
} else if (is_right_child(p)) { /* Zig-Zig -> x is right of p(x) and p(x) is right of g(x) */
rotate_left(grand_parent(x));
rotate_left(parent(x));
} else { /* Zig-Zag -> x is right of p(x) and p(x) is left of g(x) */
rotate_left(parent(x));
rotate_right(parent(x));
}
}
}
unflip(x);
}
/* ################################################# */
/* ################### TREE ######################## */
/* ################################################# */
typedef struct tree_str Tree;
struct tree_str {
Node** nodes;
int size;
};
Tree *make_tree(int size) {
Tree* tree = (Tree *)malloc(sizeof(Tree));
tree->nodes = (Node **)malloc(sizeof(Node *) * size);
int i;
for (i = 0; i < size; i++) {
tree->nodes[i] = make_node();
}
tree->size = size;
return tree;
}
void free_tree(Tree *tree) {
int i;
for (i = 0; i < tree->size; i++) {
free(tree->nodes[i]);
tree->nodes[i] = NULL;
}
free(tree->nodes);
tree->nodes = NULL;
free(tree);
tree = NULL;
}
void access(Node *v) {
/* Splays the aux tree */
splay(v);
/* Remove v's preferred child */
if (v->right) {
v->right->path_parent = v;
v->right->parent = NULL;
v->right = NULL;
}
/* Changes the preferred path to be this new one */
while (v->path_parent) {
Node *w = v->path_parent;
splay(w);
/* Switch w's preferred child from whatever it was to v */
if(w->right) {
w->right->path_parent = w;
w->right->parent = NULL;
}
w->right = v;
v->parent = w;
v->path_parent = NULL;
splay(v);
}
}
Node *get_right_most_node(Node *node) {
unflip(node);
while(node->right) {
node = node->right;
unflip(node);
}
return node;
}
void re_root(Node *new_root) {
/* just flip a bit */
access(new_root);
new_root->is_fliped = !new_root->is_fliped;
}
void cut(Tree *tree, int _v, int _w) {
Node *v = tree->nodes[_v];
Node *w = tree->nodes[_w];
if(_v == _w)
return;
/* Make v the root of the splay tree and the tree of trees (access) */
/* 1. The node which we get by following v's left node to the right (aka the node which is the parent
of v on the represented tree) */
/* 2. Now either w (after splayed) points to v through the path_pointer (w is a child of v) or.. */
access(v);
if (v->left && get_right_most_node(v->left) == w) { /* w is a parent of v */
v->left->parent = NULL;
v->left = NULL;
} else {
/* Splay to make w have a path_parent (if any exist) */
splay(w);
if (w->left == NULL && w->path_parent == v) { /* w is a child of v */
w->path_parent = NULL;
}
}
}
void link(Tree *tree, int _v, int _w) {
Node *v = tree->nodes[_v];
Node *w = tree->nodes[_w];
if (_v == _w)
return;
/* Make v the root of the represented tree (to make the link possible) */
re_root(v);
access(w);
/* This is suficient to determine if it would cause a loop. No need to do connected(...) */
if (v->parent || v->path_parent) {
return;
} else {
w->right = v;
v->parent = w;
}
}
Node *find_root(Node *v) {
access(v);
while(v->left) {
v = v->left;
}
splay(v);
return v;
}
int connected(Tree *tree, int _v, int _w) {
Node *v = tree->nodes[_v];
Node *w = tree->nodes[_w];
if (_v == _w) {
return 1;
}
re_root(v);
access(w);
/* If w 'messed' with v, making it not the root anymore, that means both are in the same aux tree, and since
v is the root, it means the are connected */
return (v->parent || v->path_parent);
}
int main() {
char command;
int arg1, arg2, size;
scanf("%d\n", &size);
Tree *tree = make_tree(size);
while (scanf("%c %d %d\n", &command, &arg1, &arg2) != EOF) {
arg1--;
arg2--;
switch (command) {
case 'L':
link(tree, arg1, arg2);
break;
case 'C':
cut(tree, arg1, arg2);
break;
case 'Q':
printf(connected(tree, arg1, arg2) ? "T\n" : "F\n");
break;
default:
printf("Unknown command %c\n", command);
exit(EXIT_FAILURE);
}
}
free_tree(tree);
return 0;
}