#include <bits/stdc++.h> using namespace std; int n; struct Node { int color; int index; }; vector<Node> nodes; vector<vector<int>> groups; vector<int> avail_colors; void SetColor(int a, int c) { nodes[a].color = c; nodes[a].index = 0; groups[c].push_back(a); } void Init() { nodes.resize(n); groups.resize(n); for (int i = 0; i < n; i++) SetColor(i, i); } void Free(int c) { groups[c].clear(); avail_colors.push_back(c); } void Fill(int a) { int c = nodes[a].color; if (c < 0) return; for (int i : groups[c]) nodes[i].color = -1; Free(c); } void Merge(int a, int b) { int ca = nodes[a].color; int cb = nodes[b].color; if (ca == cb) return Fill(a); if (groups[ca].size() < groups[cb].size()) swap(ca, cb); for (int i : groups[cb]) { nodes[i].color = ca; nodes[i].index = (int) groups[ca].size(); groups[ca].push_back(i); } Free(cb); } void Add(int a, int b) { if (nodes[a].color < 0) Fill(b); else if (nodes[b].color < 0) Fill(a); else if (a == b) Fill(a); else Merge(a, b); } void Del(int a) { int c = nodes[a].color; if (c >= 0) { int i = nodes[a].index; int b = groups[c].back(); assert(groups[c].size() >= 2); nodes[b].index = i; groups[c][i] = b; groups[c].pop_back(); } c = avail_colors.back(); avail_colors.pop_back(); SetColor(a, c); } int Query(int a) { int c = nodes[a].color; if (c < 0) return 1; else if (groups[c].size() > 1) return -1; else return 0; } int main() { int q; scanf("%d%d", &n, &q); Init(); for (; q; q--) { char c[8]; scanf("%s", c); if (*c == '+') { int a, b; scanf("%d%d", &a, &b); Add(a - 1, b - 1); } else if (*c == '-') { int c; scanf("%d", &c); Del(c - 1); } else { int d; scanf("%d", &d); int r = Query(d - 1); if (r < 0) putchar('?'); else printf("%d", r); } } putchar('\n'); 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h> using namespace std; int n; struct Node { int color; int index; }; vector<Node> nodes; vector<vector<int>> groups; vector<int> avail_colors; void SetColor(int a, int c) { nodes[a].color = c; nodes[a].index = 0; groups[c].push_back(a); } void Init() { nodes.resize(n); groups.resize(n); for (int i = 0; i < n; i++) SetColor(i, i); } void Free(int c) { groups[c].clear(); avail_colors.push_back(c); } void Fill(int a) { int c = nodes[a].color; if (c < 0) return; for (int i : groups[c]) nodes[i].color = -1; Free(c); } void Merge(int a, int b) { int ca = nodes[a].color; int cb = nodes[b].color; if (ca == cb) return Fill(a); if (groups[ca].size() < groups[cb].size()) swap(ca, cb); for (int i : groups[cb]) { nodes[i].color = ca; nodes[i].index = (int) groups[ca].size(); groups[ca].push_back(i); } Free(cb); } void Add(int a, int b) { if (nodes[a].color < 0) Fill(b); else if (nodes[b].color < 0) Fill(a); else if (a == b) Fill(a); else Merge(a, b); } void Del(int a) { int c = nodes[a].color; if (c >= 0) { int i = nodes[a].index; int b = groups[c].back(); assert(groups[c].size() >= 2); nodes[b].index = i; groups[c][i] = b; groups[c].pop_back(); } c = avail_colors.back(); avail_colors.pop_back(); SetColor(a, c); } int Query(int a) { int c = nodes[a].color; if (c < 0) return 1; else if (groups[c].size() > 1) return -1; else return 0; } int main() { int q; scanf("%d%d", &n, &q); Init(); for (; q; q--) { char c[8]; scanf("%s", c); if (*c == '+') { int a, b; scanf("%d%d", &a, &b); Add(a - 1, b - 1); } else if (*c == '-') { int c; scanf("%d", &c); Del(c - 1); } else { int d; scanf("%d", &d); int r = Query(d - 1); if (r < 0) putchar('?'); else printf("%d", r); } } putchar('\n'); return 0; } |