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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <bits/stdc++.h>
using namespace std;

// implementation of the data structure described in https://www.corelab.ntua.gr/acac10/ACAC2010_Talks/SimonYoffe.pdf
class Find_Union_Delete {
//private:
public:
    class FUD_node;
    class FUD_tree;

    int n;
    vector <FUD_node*> nodes;
    
    static void dfslist_cut(FUD_node* fi, FUD_node* la) {
        FUD_node* prefi = fi->dfs_list[0];
        FUD_node* postla = la->dfs_list[1];
        prefi->dfs_list[1] = postla;
        postla->dfs_list[0] = prefi;

        fi->dfs_list[0] = la;
        la->dfs_list[1] = fi;
    }

    static void dfslist_paste_between(FUD_node* prefi, FUD_node* postla, FUD_node* fi, FUD_node* la) {
        prefi->dfs_list[1] = fi;
        fi->dfs_list[0] = prefi;
        la->dfs_list[1] = postla;
        postla->dfs_list[0] = la;
    }

    static void dfslist_paste_after(FUD_node* prefi, FUD_node* fi, FUD_node* la) {
        dfslist_paste_between(prefi, prefi->dfs_list[1], fi, la);        
    }

    static void dfslist_paste_before(FUD_node* postla, FUD_node* fi, FUD_node* la) {
        dfslist_paste_between(postla->dfs_list[0], postla, fi, la);        
    }

    class FUD_node {
    public:
        int id;

        FUD_node* parent;
        FUD_tree* tree;

        int num_children;
        FUD_node* extreme_children[2];

        FUD_node* siblings_list[2];
        FUD_node* dfs_list[2];

        // Clean_ node to the state of a size 1 tree, doesn't change id
        void clean_(FUD_tree* _tree = NULL) {
            parent = NULL;
            tree = _tree;
            num_children = 0;
            extreme_children[0] = NULL;
            extreme_children[1] = NULL;
            siblings_list[0] = NULL;
            siblings_list[1] = NULL;
            dfs_list[0] = this;
            dfs_list[1] = this;
        }

        // Create new node as a root for a new tree
        FUD_node(int _id) {
            id = _id;
            clean_(new FUD_tree(this));
        }

        // If I am root, delete my tree as well
        ~FUD_node() {
            if (!parent)
                delete tree;
        }

        // Add other_node as my rightmost child
        // Fix ONLY structure in FUD_nodes, tree data has to be fixed outside
        void append(FUD_node* node) {
            this->num_children++;
            node->parent = this;
            
            if (num_children == 1) {
                extreme_children[0] = node;
                extreme_children[1] = node;
                node->siblings_list[0] = NULL;
                node->siblings_list[1] = NULL;
            }
            else {
                node->siblings_list[1] = NULL;
                node->siblings_list[0] = extreme_children[1];
                extreme_children[1]->siblings_list[1] = node;
                extreme_children[1] = node;
            }

            dfslist_paste_after(this, node, node->dfs_list[0]);

            //if (!parent && node->num_children > 0)
            //    tree->non_leaf_children.push(node);
            
           // if (parent && !parent->parent && this->num_children == 1)
            //    parent->tree->non_leaf_children.push(this);
        }

        void push_left_most_up() {
            FUD_node* gp = parent->parent;
            
            gp->num_children++;
            FUD_node* parents_left_sibling = parent->siblings_list[0];

            this->siblings_list[1] = parent;
            this->siblings_list[0] = parents_left_sibling;
            parent->siblings_list[0] = this;

            if (!parents_left_sibling) 
                gp->extreme_children[0] = this;
            else
                parents_left_sibling->siblings_list[1] = this;
            
            //if (!gp->parent && this->num_children > 0)
              //  tree->non_leaf_children.push(this);

            this->parent = gp;
        }

        // Detach from parent and siblings DON'T touch my structure
        void detach() {
            //assert(parent);
            parent->num_children--;
            // only child
            if (parent->num_children == 0) {
                parent->extreme_children[0] = NULL;
                parent->extreme_children[1] = NULL;
                return;
            }
            // extreme child
            for (int side = 0; side <= 1; side++) {
                if (parent->extreme_children[side] == this) {
                    parent->extreme_children[side] = siblings_list[(side + 1) % 2];
                    parent->extreme_children[side]->siblings_list[side] = NULL;
                    return;
                }
            }
            // middle child
            siblings_list[0]->siblings_list[1] = siblings_list[1];
            siblings_list[1]->siblings_list[0] = siblings_list[0];
        }

        // Split path for non left-most children
        void split_normal() {
            //assert(parent);
            //assert(parent->parent);
            //assert(siblings_list[0]);
            FUD_node* gp = parent->parent;

            detach();
            dfslist_cut(this, siblings_list[0]->dfs_list[0]);
            gp->append(this);
        }

        // Split path for left-most children
        void split_left_most() {
            //assert(parent);
            //assert(parent->parent);
            //assert(!siblings_list[0]);
            //assert(parent->extreme_children[0] == this);
            //FUD_node* gp = parent->parent;

            detach();
            push_left_most_up();
            // no need to fix dfslist
        }

        // Split path (more flexible compression)
        void split_path() {
            if (!parent) return;
            if (!parent->parent) return;

            if (siblings_list[0]) 
                split_normal();
            else
                split_left_most();

            if (parent->num_children > 0 && parent->num_children < 3) 
                parent->extreme_children[0]->split_path();
        }

        // Find nodes tree
        FUD_tree* Find(bool use_split = true) {
            if (!parent) return tree;
            FUD_tree* ret = parent->Find();
            if (use_split) split_path();
            return ret;
        }

        void swap_data(FUD_node* other) {
            swap(this->id, other->id);
        }
    };

    class FUD_tree {
    public:
        FUD_node* root;
        int size;
        int data;
        //queue <FUD_node*> non_leaf_children;

        // Create a size 1 tree for given node
        FUD_tree(FUD_node* _root) {
            root = _root;
            data = 1;
            size = 1;
        }

        // Get all nodes in tree and save them to a vector
        void get_all_nodes(vector <FUD_node*> &v) {
            v.push_back(root);
            FUD_node* curr = root->dfs_list[1];
            while (curr != root) {
                v.push_back(curr);
                curr = curr->dfs_list[1];
            }
        }

        // Get a non-leaf root child
        FUD_node* get_non_leaf_child() {
            ////assert(!non_leaf_children.empty());
            //if (non_leaf_children.empty()) {
                FUD_node* ret = root->extreme_children[0];
                while (ret->num_children == 0)
                    ret = ret->siblings_list[1];
                return ret;
            //}
            //FUD_node* node = non_leaf_children.front();
            //while (node->Find() != this || node->num_children == 0) {
            //    non_leaf_children.pop();
            //    node = non_leaf_children.front();
            //}
            //non_leaf_children.pop();
            //assert(node);
            //assert(node->num_children > 0);
            //assert(node->parent == root);
            //return node;
        }

        // Union for other->size <= 3
        void Union_small(FUD_tree* other) {
            vector <FUD_node*> other_nodes;
            other->get_all_nodes(other_nodes);

            //assert(other_nodes[0] == other->root);

            for (FUD_node* node : other_nodes) {
                node->clean_();
                root->append(node);
            }
        }

        // Union for other->size >= 4
        void Union_big(FUD_tree* other) {
            root->append(other->root);
            //assert(!non_leaf_children.empty());
            //assert(root->num_children >= 3);
        }

        // Union this and other, delete other
        void Union(FUD_tree* other) {
            //assert(this != other);
            if (size < other->size) {
                other->Union(this);
                return;
            }

            this->size += other->size;
            if (other->size <= 3) Union_small(other);
            else Union_big(other);
            
            delete other;
        }

        // Delete for size <= 4 (after deletion)
        void Delete_small(FUD_node* node) {
            //assert(node->dfs_list[1] != node);
            FUD_node* new_root = root;
            if (new_root == node)
                new_root = node->dfs_list[1];
            
            vector <FUD_node*> all_nodes;
            get_all_nodes(all_nodes);
            
            vector <FUD_node*> new_root_children;
            for (FUD_node* inode : all_nodes) {
                if (inode != new_root && inode != node) 
                    new_root_children.push_back(inode);
            }

            root = new_root;
            //while (non_leaf_children.size()) 
            //    non_leaf_children.pop();

            new_root->clean_(this);
            for (FUD_node* child : new_root_children) {
                child->clean_();
                root->append(child);
            }
        }

        // Delete for size >= 5 (after deletion)
        FUD_node* Delete_big(FUD_node* node) {
            FUD_node* leaf = root->dfs_list[0];
            //assert(leaf->num_children == 0);
            //assert(leaf->Find(false) == this);
            //assert(leaf->Find(false)->size >= 4);
            //assert(leaf->Find(false)->root->num_children >= 3);
            if (leaf->parent->num_children < 3)
                leaf->split_path();
            //assert(leaf->parent->num_children >= 3);

            leaf->swap_data(node);

            leaf->detach();
            dfslist_cut(leaf, leaf);

            //assert(leaf->parent->num_children >= 2);
            if (leaf->parent->num_children == 2) {
            //if (leaf->parent->num_children > 0 && leaf->parent->num_children < 3) {
                if (leaf->parent == root) {
                    FUD_node* nlc = get_non_leaf_child();
                    nlc->extreme_children[0]->split_path();
                }
                else {
                    leaf->parent->extreme_children[0]->split_path();
                }
            }

            return leaf;
        }
        
        // Delete node from tree, create a new tree for it
        // Return swapped node if there is one
        FUD_node* Delete(FUD_node* node) {
            //assert(size > 1);
            FUD_node* ret = node;
            size--;
            if (size <= 4) 
                Delete_small(node);
            else
                ret = Delete_big(node);
            
            ret->clean_(new FUD_tree(ret));
            return ret;
        }
    };

//public:

    Find_Union_Delete(int _n) {
        n = _n;
        for (int i = 0; i < n; i++)
            nodes.push_back(new FUD_node(i));
    }

    ~Find_Union_Delete() {
        for (FUD_node* node : nodes)
            delete node;
    }

    int Find(int a) {
        return nodes[a]->Find()->root->id;
    }

    void Union(int a, int b) {
        if (Find(a) == Find(b)) {
            //assert(nodes[a]->Find()->data != 3);
            nodes[a]->Find()->data = 3;
            return;
        }

        int new_data = 2;
        if (nodes[a]->Find()->data == 3 || nodes[b]->Find()->data == 3)
            new_data = 3;

        nodes[a]->Find()->Union(nodes[b]->Find());
        nodes[a]->Find()->data = new_data;
    }

    void Delete(int a) {
        if (nodes[a]->Find()->size == 1) {
            //assert(nodes[a]->Find()->data == 3);
            nodes[a]->Find()->data = 1;
            return;
        }

        if (nodes[a]->Find()->size == 2) {
            bool wasdata3 = (nodes[a]->Find()->data == 3);
            int b = nodes[a]->Find()->root->id;
            if (b == a) 
                b = nodes[b]->dfs_list[1]->id;
                
            FUD_tree* old_tree = nodes[a]->Find();
            nodes[a]->clean_(new FUD_tree(nodes[a]));
            nodes[b]->clean_(new FUD_tree(nodes[b]));
            delete old_tree;

            if (wasdata3)
                Union(b, b);
            return;
        }

        FUD_node* swapped_node = nodes[a]->Find()->Delete(nodes[a]);
        swap(nodes[swapped_node->id], nodes[nodes[a]->id]);
    }

    int get_root_data(int a) {
        return nodes[a]->Find()->data;
    }
};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, q;
    cin >> n >> q;

    Find_Union_Delete fud(n + 1);

    while (q--) {
        char t;
        int a, b;
        cin >> t;

        switch (t)
        {
        case '?':
            cin >> a;
            b = fud.get_root_data(a);
            if (b == 1) cout << "0";
            if (b == 2) cout << "?";
            if (b == 3) cout << "1";
            //cout << "\n";
            //cout << flush;
            break;

        case '+':
            cin >> a >> b;
            fud.Union(a, b);
            break;

        case '-':
            cin >> a;
            fud.Delete(a);
            break;
        }
    }
    
    cout << "\n";
    return 0;
}