/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct DSU { int n, q; vector<int> rep; vector<list<int> > spojna; vector<bool> owner; vector<int> curr_ind; vector<int> rev; vector<int> known; int used; DSU(int _n, int _q) { n = _n; q = _q; rep.resize(n + q); spojna.resize(n + q); owner.resize(n); curr_ind.resize(n); rev.resize(n + q); known.resize(n + q); REP(i, n+q) { rep[i] = i; spojna[i] = {i}; rev[i] = i; } REP(i, n) { curr_ind[i] = i; } used = n; } int find_rep(int a) { if (a == rep[a]) { return a; } rep[a] = find_rep(rep[a]); return rep[a]; } void union_rep(int a, int b) { a = find_rep(a); b = find_rep(b); if (a == b) { return; } if (ssize(spojna[a]) < ssize(spojna[b])) { swap(a, b); } spojna[a].splice(spojna[a].begin(), spojna[b]); known[a] += known[b]; known[b] = 0; rep[b] = a; } void disassemble(int a, bool owned=true) { a = find_rep(a); list<int> tmp; tmp.splice(tmp.begin(), spojna[a]); for (int v : tmp) { rep[v] = v; spojna[v] = {v}; known[v] = 0; if (v == curr_ind[rev[v]]) { owner[rev[v]] = owned; } } } void add_edge(int a, int b) { if (owner[a] || owner[b]) { disassemble(curr_ind[a]); disassemble(curr_ind[b]); return; } if (find_rep(curr_ind[a]) == find_rep(curr_ind[b])) { disassemble(curr_ind[a]); return; } union_rep(curr_ind[a], curr_ind[b]); } void remove(int a) { owner[a] = false; int x = find_rep(curr_ind[a]); known[x]++; // if (a == 1) { // debug(x, known[x], ssize(spojna[x])); // } if (known[x] + 1 >= ssize(spojna[x])) { disassemble(x, false); } curr_ind[a] = used; rev[used] = a; used++; } void answer(int a) { if (owner[a]) { cout << "1"; return; } int x = find_rep(curr_ind[a]); if (known[x] + 1 >= ssize(spojna[x])) { cout << "0"; } else { cout << "?"; } } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; DSU dsu(n, q); while (q--) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; a--, b--; dsu.add_edge(a, b); } else if (c == '-') { int a; cin >> a; a--; dsu.remove(a); } else if (c == '?') { int a; cin >> a; a--; dsu.answer(a); } } cout << "\n"; }
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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct DSU { int n, q; vector<int> rep; vector<list<int> > spojna; vector<bool> owner; vector<int> curr_ind; vector<int> rev; vector<int> known; int used; DSU(int _n, int _q) { n = _n; q = _q; rep.resize(n + q); spojna.resize(n + q); owner.resize(n); curr_ind.resize(n); rev.resize(n + q); known.resize(n + q); REP(i, n+q) { rep[i] = i; spojna[i] = {i}; rev[i] = i; } REP(i, n) { curr_ind[i] = i; } used = n; } int find_rep(int a) { if (a == rep[a]) { return a; } rep[a] = find_rep(rep[a]); return rep[a]; } void union_rep(int a, int b) { a = find_rep(a); b = find_rep(b); if (a == b) { return; } if (ssize(spojna[a]) < ssize(spojna[b])) { swap(a, b); } spojna[a].splice(spojna[a].begin(), spojna[b]); known[a] += known[b]; known[b] = 0; rep[b] = a; } void disassemble(int a, bool owned=true) { a = find_rep(a); list<int> tmp; tmp.splice(tmp.begin(), spojna[a]); for (int v : tmp) { rep[v] = v; spojna[v] = {v}; known[v] = 0; if (v == curr_ind[rev[v]]) { owner[rev[v]] = owned; } } } void add_edge(int a, int b) { if (owner[a] || owner[b]) { disassemble(curr_ind[a]); disassemble(curr_ind[b]); return; } if (find_rep(curr_ind[a]) == find_rep(curr_ind[b])) { disassemble(curr_ind[a]); return; } union_rep(curr_ind[a], curr_ind[b]); } void remove(int a) { owner[a] = false; int x = find_rep(curr_ind[a]); known[x]++; // if (a == 1) { // debug(x, known[x], ssize(spojna[x])); // } if (known[x] + 1 >= ssize(spojna[x])) { disassemble(x, false); } curr_ind[a] = used; rev[used] = a; used++; } void answer(int a) { if (owner[a]) { cout << "1"; return; } int x = find_rep(curr_ind[a]); if (known[x] + 1 >= ssize(spojna[x])) { cout << "0"; } else { cout << "?"; } } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; DSU dsu(n, q); while (q--) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; a--, b--; dsu.add_edge(a, b); } else if (c == '-') { int a; cin >> a; a--; dsu.remove(a); } else if (c == '?') { int a; cin >> a; a--; dsu.answer(a); } } cout << "\n"; } |