#pragma GCC optimize("O3,unroll-loops") //#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //a poem about frogs #include <bits/stdc++.h> #define st first #define nd second #define eb emplace_back #define ll long long #define vi vector<int> #define vvi vector<vector<int>> #define mp make_pair #define ii pair<int, int> #define all(a) a.begin(), a.end() #define fastio() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define rep(a, b) for (int a = 0; a < b; ++a) using namespace std; const int maxn=3e5; vi sz(maxn+1, 1); vi par(maxn+1); vvi children(maxn); vi stan(maxn, 0); //0 bez kompa, 1 ambigous, 2 z kompem vi zero_count(maxn); void __sanity_check(int v) { if (v == par[v]) { if (sz[v] == 1) { if (stan[v] == 1) { stan[v] = 0; } } } } int __find(int a) { if (par[a] == a) return a; return __find(par[a]); } void __remove(const int v, const int big_par=3e5) { //remove a single vertex const int chsz = children[v].size(); if (v == par[v]) { if (chsz >= 1) { const int new_par = children[v][0]; par[new_par] = new_par; sz[new_par] = sz[v]-1; for (int i = 1; i < chsz; ++i) { par[children[v][i]] = new_par; children[new_par].eb(children[v][i]); sz[new_par] += sz[children[v][i]]; } //__sanity_check(new_par); } } else { for (int i : children[v]) { par[i] = par[v]; children[par[v]].eb(i); } --sz[big_par]; } par[v] = v; sz[v] = 1; children[v] = vi(0); stan[v] = 0; } void dfs(int cur, const int big_par) { //recursively, remove the ambiguity from the entire skladowa for (int next : children[cur]) dfs(next, big_par); __remove(cur, big_par); stan[cur] = 2; } void __union(int a, int b) { a = __find(a), b = __find(b); if (stan[a] == 2) { dfs(b, b); cerr << "y\n"; return; } if (stan[b] == 2) { dfs(a, a); cerr << "n\n"; return; } stan[a] = stan[b] = 1; if (a == b) { dfs(a, a); } else { if (sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; par[b] = a; children[a].eb(b); } } int ask(int v) { //0 - nie ma, 1 - ambigous, 2 - komp return stan[v]; } int main() { int n, q; cin >> n >> q; iota(all(par), 0); fill(all(sz), 1); //rep(i, maxn) skladowa[i] = vi{i}; rep(i, q) { char c; int u; cin >> c >> u; --u; if (c == '?') { int res = ask(u); if (res == 0) cout << '0'; else if (res == 1) cout << '?'; else cout << '1'; } else if (c == '+') { int v; cin >> v; --v; __union(u, v); } else { __remove(u); } } cout << '\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 | #pragma GCC optimize("O3,unroll-loops") //#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //a poem about frogs #include <bits/stdc++.h> #define st first #define nd second #define eb emplace_back #define ll long long #define vi vector<int> #define vvi vector<vector<int>> #define mp make_pair #define ii pair<int, int> #define all(a) a.begin(), a.end() #define fastio() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define rep(a, b) for (int a = 0; a < b; ++a) using namespace std; const int maxn=3e5; vi sz(maxn+1, 1); vi par(maxn+1); vvi children(maxn); vi stan(maxn, 0); //0 bez kompa, 1 ambigous, 2 z kompem vi zero_count(maxn); void __sanity_check(int v) { if (v == par[v]) { if (sz[v] == 1) { if (stan[v] == 1) { stan[v] = 0; } } } } int __find(int a) { if (par[a] == a) return a; return __find(par[a]); } void __remove(const int v, const int big_par=3e5) { //remove a single vertex const int chsz = children[v].size(); if (v == par[v]) { if (chsz >= 1) { const int new_par = children[v][0]; par[new_par] = new_par; sz[new_par] = sz[v]-1; for (int i = 1; i < chsz; ++i) { par[children[v][i]] = new_par; children[new_par].eb(children[v][i]); sz[new_par] += sz[children[v][i]]; } //__sanity_check(new_par); } } else { for (int i : children[v]) { par[i] = par[v]; children[par[v]].eb(i); } --sz[big_par]; } par[v] = v; sz[v] = 1; children[v] = vi(0); stan[v] = 0; } void dfs(int cur, const int big_par) { //recursively, remove the ambiguity from the entire skladowa for (int next : children[cur]) dfs(next, big_par); __remove(cur, big_par); stan[cur] = 2; } void __union(int a, int b) { a = __find(a), b = __find(b); if (stan[a] == 2) { dfs(b, b); cerr << "y\n"; return; } if (stan[b] == 2) { dfs(a, a); cerr << "n\n"; return; } stan[a] = stan[b] = 1; if (a == b) { dfs(a, a); } else { if (sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; par[b] = a; children[a].eb(b); } } int ask(int v) { //0 - nie ma, 1 - ambigous, 2 - komp return stan[v]; } int main() { int n, q; cin >> n >> q; iota(all(par), 0); fill(all(sz), 1); //rep(i, maxn) skladowa[i] = vi{i}; rep(i, q) { char c; int u; cin >> c >> u; --u; if (c == '?') { int res = ask(u); if (res == 0) cout << '0'; else if (res == 1) cout << '?'; else cout << '1'; } else if (c == '+') { int v; cin >> v; --v; __union(u, v); } else { __remove(u); } } cout << '\n'; return 0; } |