#include <cstdio> #include <vector> #include <algorithm> #include <map> #include <unordered_map> using namespace std; const int N = 300000 + 9; const int Q = 1000000 + 9; int nextInt() { int n; scanf("%d", &n); return n; } int fuId[N]; class FU { struct Node { int parent; int logicalSize; int next; int orgId; }; vector<Node> v; public: FU() { v.reserve(N + Q); } int NewItem(int orgId) { int id = v.size(); v.push_back({ -1, 1, id, orgId }); return id; } int Find(int x) { int r = x, tmp; while (v[r].parent >= 0) r = v[r].parent; while (x != r) { tmp = v[x].parent; v[x].parent = r; x = tmp; } return r; } void UnionGroups(int x, int y) { // assert(x, y are different roots); if (v[x].parent < v[y].parent) { v[x].logicalSize += v[y].logicalSize; v[x].parent += v[y].parent; v[y].parent = x; } else { v[y].logicalSize += v[x].logicalSize; v[y].parent += v[x].parent; v[x].parent = y; } int xx = v[x].next; int yy = v[y].next; // before: // x -> xx -...-> x // y -> yy -...-> y // result: // x -> yy -...-> y -> xx -...-> x v[x].next = yy; v[y].next = xx; } int GroupSize(int x) { return v[Find(x)].logicalSize; } void EraseGroup(int x) { int src = x; do { x = v[x].next; if (v[x].orgId >= 0) fuId[v[x].orgId] = -1; } while (x != src); } void EraseItem(int x) { v[x].orgId = -1; --v[Find(x)].logicalSize; } }; FU fu; class Solution { public: void init(int n) { for (int i = 1; i <= n; ++i) fuId[i] = fu.NewItem(i); } void add(int a, int b) { if (fuId[a] < 0) return fu.EraseGroup(fuId[b]); if (fuId[b] < 0) return fu.EraseGroup(fuId[a]); int aa = fu.Find(fuId[a]); int bb = fu.Find(fuId[b]); if (aa != bb) return fu.UnionGroups(aa, bb); fu.EraseGroup(aa); } void erase(int a) { if (fuId[a] >= 0) fu.EraseItem(fuId[a]); fuId[a] = fu.NewItem(a); } int check(int a) { if (fuId[a] < 0) return 1; if (fu.GroupSize(fuId[a]) == 1) return 0; return 2; } }; Solution sol; int main() { int n = nextInt(); sol.init(n); int q = nextInt(); for (int i = 0; i < q; ++i) { char cmd[64]; scanf("%s", cmd); if ('+' == cmd[0] || '1' == cmd[0]) { int a = nextInt(); int b = nextInt(); sol.add(a, b); continue; } if ('-' == cmd[0] || '2' == cmd[0]) { int c = nextInt(); sol.erase(c); continue; } if ('?' == cmd[0] || '3' == cmd[0]) { int d = nextInt(); int res = sol.check(d); printf("%c", "01?"[res]); continue; } // assert(false); // unknown command! } printf("\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 139 140 141 142 | #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <unordered_map> using namespace std; const int N = 300000 + 9; const int Q = 1000000 + 9; int nextInt() { int n; scanf("%d", &n); return n; } int fuId[N]; class FU { struct Node { int parent; int logicalSize; int next; int orgId; }; vector<Node> v; public: FU() { v.reserve(N + Q); } int NewItem(int orgId) { int id = v.size(); v.push_back({ -1, 1, id, orgId }); return id; } int Find(int x) { int r = x, tmp; while (v[r].parent >= 0) r = v[r].parent; while (x != r) { tmp = v[x].parent; v[x].parent = r; x = tmp; } return r; } void UnionGroups(int x, int y) { // assert(x, y are different roots); if (v[x].parent < v[y].parent) { v[x].logicalSize += v[y].logicalSize; v[x].parent += v[y].parent; v[y].parent = x; } else { v[y].logicalSize += v[x].logicalSize; v[y].parent += v[x].parent; v[x].parent = y; } int xx = v[x].next; int yy = v[y].next; // before: // x -> xx -...-> x // y -> yy -...-> y // result: // x -> yy -...-> y -> xx -...-> x v[x].next = yy; v[y].next = xx; } int GroupSize(int x) { return v[Find(x)].logicalSize; } void EraseGroup(int x) { int src = x; do { x = v[x].next; if (v[x].orgId >= 0) fuId[v[x].orgId] = -1; } while (x != src); } void EraseItem(int x) { v[x].orgId = -1; --v[Find(x)].logicalSize; } }; FU fu; class Solution { public: void init(int n) { for (int i = 1; i <= n; ++i) fuId[i] = fu.NewItem(i); } void add(int a, int b) { if (fuId[a] < 0) return fu.EraseGroup(fuId[b]); if (fuId[b] < 0) return fu.EraseGroup(fuId[a]); int aa = fu.Find(fuId[a]); int bb = fu.Find(fuId[b]); if (aa != bb) return fu.UnionGroups(aa, bb); fu.EraseGroup(aa); } void erase(int a) { if (fuId[a] >= 0) fu.EraseItem(fuId[a]); fuId[a] = fu.NewItem(a); } int check(int a) { if (fuId[a] < 0) return 1; if (fu.GroupSize(fuId[a]) == 1) return 0; return 2; } }; Solution sol; int main() { int n = nextInt(); sol.init(n); int q = nextInt(); for (int i = 0; i < q; ++i) { char cmd[64]; scanf("%s", cmd); if ('+' == cmd[0] || '1' == cmd[0]) { int a = nextInt(); int b = nextInt(); sol.add(a, b); continue; } if ('-' == cmd[0] || '2' == cmd[0]) { int c = nextInt(); sol.erase(c); continue; } if ('?' == cmd[0] || '3' == cmd[0]) { int d = nextInt(); int res = sol.check(d); printf("%c", "01?"[res]); continue; } // assert(false); // unknown command! } printf("\n"); return 0; } |