#include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() //#define _DEBUG using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 300'002; const int mod = 1e9 + 7; int v_mapping[N]; vector<vi> Adj; vector<int> dsu; vector<int> cnt; vector<bool> cyc; int findp(int v) { if (dsu[v] == -1) return v; return dsu[v] = findp(dsu[v]); } bool unify(int a, int b) { a = findp(a); b = findp(b); if (a == b) return true; dsu[a] = b; cnt[b] += cnt[a]; return false; } void dfs(int v) { cyc[v] = true; for (int u : Adj[v]) { if (!cyc[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; Adj.resize(n); dsu.assign(n, -1); cyc.assign(n, false); cnt.resize(n); for (int i = 0; i < n; i++) v_mapping[i] = i; while (q--) { char t; cin >> t; if (t == '+') { int a, b; cin >> a >> b; a--; b--; int v = v_mapping[a]; int u = v_mapping[b]; if (unify(v, u)) { dfs(v); } else { Adj[v].push_back(u); Adj[u].push_back(v); if (cyc[v] && !cyc[u]) dfs(u); else if (cyc[u] && !cyc[v]) dfs(v); cnt[findp(v)]++; } } else if (t == '-') { int a; cin >> a; a--; int v = v_mapping[a]; cnt[findp(v)]--; Adj.emplace_back(); v_mapping[a] = sz(Adj) - 1; cyc.push_back(false); dsu.push_back(-1); cnt.push_back(0); } else { int a; cin >> a; a--; int v = v_mapping[a]; #ifdef _DEBUG cout << a + 1 << ": "; #endif if (cyc[v]) { cout << "1"; } else if (cnt[findp(v)] == 0) { cout << "0"; } else { cout << "?"; } #ifdef _DEBUG cout << "\n"; #endif } } 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 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() //#define _DEBUG using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 300'002; const int mod = 1e9 + 7; int v_mapping[N]; vector<vi> Adj; vector<int> dsu; vector<int> cnt; vector<bool> cyc; int findp(int v) { if (dsu[v] == -1) return v; return dsu[v] = findp(dsu[v]); } bool unify(int a, int b) { a = findp(a); b = findp(b); if (a == b) return true; dsu[a] = b; cnt[b] += cnt[a]; return false; } void dfs(int v) { cyc[v] = true; for (int u : Adj[v]) { if (!cyc[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; Adj.resize(n); dsu.assign(n, -1); cyc.assign(n, false); cnt.resize(n); for (int i = 0; i < n; i++) v_mapping[i] = i; while (q--) { char t; cin >> t; if (t == '+') { int a, b; cin >> a >> b; a--; b--; int v = v_mapping[a]; int u = v_mapping[b]; if (unify(v, u)) { dfs(v); } else { Adj[v].push_back(u); Adj[u].push_back(v); if (cyc[v] && !cyc[u]) dfs(u); else if (cyc[u] && !cyc[v]) dfs(v); cnt[findp(v)]++; } } else if (t == '-') { int a; cin >> a; a--; int v = v_mapping[a]; cnt[findp(v)]--; Adj.emplace_back(); v_mapping[a] = sz(Adj) - 1; cyc.push_back(false); dsu.push_back(-1); cnt.push_back(0); } else { int a; cin >> a; a--; int v = v_mapping[a]; #ifdef _DEBUG cout << a + 1 << ": "; #endif if (cyc[v]) { cout << "1"; } else if (cnt[findp(v)] == 0) { cout << "0"; } else { cout << "?"; } #ifdef _DEBUG cout << "\n"; #endif } } cout << '\n'; } |