//Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; struct FindUnion { vector<int> P, R; int size; FindUnion(int size) { P = vector<int>(size), R = vector<int>(size); iota(P.begin(), P.end(), 0); this->size = size; } int find(int v) { if(P[v] == v) return v; return P[v] = find(P[v]); } void unify(int a, int b) { a = find(a), b = find(b); if(R[a] > R[b]) P[b] = a; else { P[a] = b; R[a] += (R[a] == R[b]); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = intin, q = intin; vector<int> state(n); FindUnion findUnion(n); vector<unordered_set<int>> G(n); vector<int> indexInFu(n); iota(indexInFu.begin(), indexInFu.end(), 0); while(q--) { char type = *istream_iterator<char>(cin); if(type == '+') { int a = intin - 1, b = intin - 1; if(state[a] == 1) a = b; if(state[b] == 1) b = a; if(findUnion.find(indexInFu[a]) != findUnion.find(indexInFu[b])) { findUnion.unify(indexInFu[a], indexInFu[b]); state[a] = 2; state[b] = 2; G[a].insert(b); G[b].insert(a); } else { function<void(int, int)> DFS = [&] (int v, int p) { findUnion.P[indexInFu[v]] = indexInFu[v]; findUnion.R[indexInFu[v]] = 0; state[v] = 1; for(const auto &u : G[v]) { if(u == p) continue; DFS(u, v); } G[v] = unordered_set<int>(); }; DFS(a, -1); } } else if(type == '-') { int c = intin - 1; if(state[c] == 2) { int newIndexInFu = findUnion.size++; findUnion.P.push_back(newIndexInFu); findUnion.R.push_back(0); if(G[c].size() == 1) { for(const auto &neighbor : G[c]) { G[neighbor].erase(c); if(G[neighbor].empty()) state[neighbor] = 0; } } else { int randomNeighbor = *G[c].begin(); G[randomNeighbor].erase(c); for(const auto &neighbor : G[c]) { if(neighbor == randomNeighbor) continue; G[neighbor].erase(c); G[neighbor].insert(randomNeighbor); G[randomNeighbor].insert(neighbor); } } G[c] = unordered_set<int>(); indexInFu[c] = newIndexInFu; } state[c] = 0; } else if(type == '?') { int d = intin - 1; cout << (state[d] == 2 ? "?" : to_string(state[d])); } } 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 | //Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; struct FindUnion { vector<int> P, R; int size; FindUnion(int size) { P = vector<int>(size), R = vector<int>(size); iota(P.begin(), P.end(), 0); this->size = size; } int find(int v) { if(P[v] == v) return v; return P[v] = find(P[v]); } void unify(int a, int b) { a = find(a), b = find(b); if(R[a] > R[b]) P[b] = a; else { P[a] = b; R[a] += (R[a] == R[b]); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = intin, q = intin; vector<int> state(n); FindUnion findUnion(n); vector<unordered_set<int>> G(n); vector<int> indexInFu(n); iota(indexInFu.begin(), indexInFu.end(), 0); while(q--) { char type = *istream_iterator<char>(cin); if(type == '+') { int a = intin - 1, b = intin - 1; if(state[a] == 1) a = b; if(state[b] == 1) b = a; if(findUnion.find(indexInFu[a]) != findUnion.find(indexInFu[b])) { findUnion.unify(indexInFu[a], indexInFu[b]); state[a] = 2; state[b] = 2; G[a].insert(b); G[b].insert(a); } else { function<void(int, int)> DFS = [&] (int v, int p) { findUnion.P[indexInFu[v]] = indexInFu[v]; findUnion.R[indexInFu[v]] = 0; state[v] = 1; for(const auto &u : G[v]) { if(u == p) continue; DFS(u, v); } G[v] = unordered_set<int>(); }; DFS(a, -1); } } else if(type == '-') { int c = intin - 1; if(state[c] == 2) { int newIndexInFu = findUnion.size++; findUnion.P.push_back(newIndexInFu); findUnion.R.push_back(0); if(G[c].size() == 1) { for(const auto &neighbor : G[c]) { G[neighbor].erase(c); if(G[neighbor].empty()) state[neighbor] = 0; } } else { int randomNeighbor = *G[c].begin(); G[randomNeighbor].erase(c); for(const auto &neighbor : G[c]) { if(neighbor == randomNeighbor) continue; G[neighbor].erase(c); G[neighbor].insert(randomNeighbor); G[randomNeighbor].insert(neighbor); } } G[c] = unordered_set<int>(); indexInFu[c] = newIndexInFu; } state[c] = 0; } else if(type == '?') { int d = intin - 1; cout << (state[d] == 2 ? "?" : to_string(state[d])); } } return 0; } |