#include<iostream> #include<vector> using namespace std; struct node { int elem = 0; node* parent = NULL; vector<node*> children; int height = 1; int count = 1; bool deleted = false; }; int imem = 0; vector<node> mem(2'100'000); node* find(vector<node*> &nodes, int x) { node* p = nodes[x]; while (p->parent != NULL) { p = p->parent; } return p; } void unions(node* na, node* nb) { if (na->height < nb->height) { na->parent = nb; nb->children.push_back(na); nb->count += na->count; } else { nb->parent = na; na->children.push_back(nb); na->count += nb->count; if (na->height = nb->height) { na->height++; } } } void dfs(vector<char> &comp, vector<node*> &nodes, node* na, char c) { if (! na->deleted) { int i = na->elem; comp[i] = c; nodes[i] = &mem[imem++]; nodes[i]->elem = i; } for (node* e : na->children) { dfs(comp, nodes, e, c); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; vector<char> comp(n + 1, '0'); vector<node*> nodes(n + 1); for (int i = 0; i <= n; i++) { nodes[i] = &mem[imem++]; nodes[i]->elem = i; } while (q--) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; if (comp[a] == '1' || comp[b] == '1') { node* na = find(nodes, a); node* nb = find(nodes, b); dfs(comp, nodes, na, '1'); dfs(comp, nodes, nb, '1'); } else { node* na = find(nodes, a); node* nb = find(nodes, b); if (na == nb) { dfs(comp, nodes, na, '1'); } else { unions(na, nb); comp[a] = '?'; comp[b] = '?'; } } } else if (c == '-') { int a; cin >> a; nodes[a]->deleted = true; comp[a] = '0'; node* na = find(nodes, a); if (na->count > 1) { na->count--; if (na->count == 1) { dfs(comp, nodes, na, '0'); } } nodes[a] = &mem[imem++]; nodes[a]->elem = a; } else { int a; cin >> a; cout << comp[a]; } } cout << endl; return 0; }
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 | #include<iostream> #include<vector> using namespace std; struct node { int elem = 0; node* parent = NULL; vector<node*> children; int height = 1; int count = 1; bool deleted = false; }; int imem = 0; vector<node> mem(2'100'000); node* find(vector<node*> &nodes, int x) { node* p = nodes[x]; while (p->parent != NULL) { p = p->parent; } return p; } void unions(node* na, node* nb) { if (na->height < nb->height) { na->parent = nb; nb->children.push_back(na); nb->count += na->count; } else { nb->parent = na; na->children.push_back(nb); na->count += nb->count; if (na->height = nb->height) { na->height++; } } } void dfs(vector<char> &comp, vector<node*> &nodes, node* na, char c) { if (! na->deleted) { int i = na->elem; comp[i] = c; nodes[i] = &mem[imem++]; nodes[i]->elem = i; } for (node* e : na->children) { dfs(comp, nodes, e, c); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; vector<char> comp(n + 1, '0'); vector<node*> nodes(n + 1); for (int i = 0; i <= n; i++) { nodes[i] = &mem[imem++]; nodes[i]->elem = i; } while (q--) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; if (comp[a] == '1' || comp[b] == '1') { node* na = find(nodes, a); node* nb = find(nodes, b); dfs(comp, nodes, na, '1'); dfs(comp, nodes, nb, '1'); } else { node* na = find(nodes, a); node* nb = find(nodes, b); if (na == nb) { dfs(comp, nodes, na, '1'); } else { unions(na, nb); comp[a] = '?'; comp[b] = '?'; } } } else if (c == '-') { int a; cin >> a; nodes[a]->deleted = true; comp[a] = '0'; node* na = find(nodes, a); if (na->count > 1) { na->count--; if (na->count == 1) { dfs(comp, nodes, na, '0'); } } nodes[a] = &mem[imem++]; nodes[a]->elem = a; } else { int a; cin >> a; cout << comp[a]; } } cout << endl; return 0; } |