#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair //#define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); set<int> g[MAX]; int p[MAX]; int siz[MAX]; int good[MAX]; set<int> idx; vector<int> comp; void create(int v) { p[v] = *idx.begin(); idx.erase(p[v]); siz[p[v]] = 1; } void dfs(int v, int par = 0) { comp.pb(v); for(auto to : g[v]) { if(to == par) continue; dfs(to, v); } } void del_set(int i) { idx.insert(i); siz[i] = 0; } void unite(int u, int v) { if(siz[p[u]] < siz[p[v]]) swap(u, v); ///v - smaller set dfs(v); siz[p[u]] += siz[p[v]]; del_set(p[v]); for(auto x : comp) { p[x] = p[u]; } comp.clear(); g[u].insert(v); g[v].insert(u); } void make_good(int v) { dfs(v); int par = p[v]; del_set(par); for(auto u : comp) { good[u] = 1; g[u].clear(); create(u); } for(auto u : comp) { assert(sz(g[u]) == 0); assert(siz[p[u]] == 1); } comp.clear(); } void solve() { int n, q; cin >> n >> q; for(int i = 1; i <= n; i++) idx.insert(i); for(int i = 1; i <= n; i++) create(i); string ans; while(q--) { char c; cin >> c; if(c == '+') { int u, v; cin >> u >> v; if(good[u]) make_good(v); else if(good[v]) make_good(u); else { if(p[u] == p[v]) { make_good(v); } else unite(u, v); } } else if(c == '-') { int v; cin >> v; if(good[v]) { assert(siz[p[v]] == 1); good[v] = 0; } else { int par = p[v]; assert(siz[par] != 1); siz[par]--; int prev = 0; for(auto to : g[v]) { if(prev) { g[prev].insert(to); g[to].insert(prev); } g[to].erase(v); prev = to; } g[v].clear(); create(v); } } else { int v; cin >> v; if(good[v]) ans.pb('1'); else if(siz[p[v]] == 1) ans.pb('0'); else ans.pb('?'); } } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } }
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair //#define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); set<int> g[MAX]; int p[MAX]; int siz[MAX]; int good[MAX]; set<int> idx; vector<int> comp; void create(int v) { p[v] = *idx.begin(); idx.erase(p[v]); siz[p[v]] = 1; } void dfs(int v, int par = 0) { comp.pb(v); for(auto to : g[v]) { if(to == par) continue; dfs(to, v); } } void del_set(int i) { idx.insert(i); siz[i] = 0; } void unite(int u, int v) { if(siz[p[u]] < siz[p[v]]) swap(u, v); ///v - smaller set dfs(v); siz[p[u]] += siz[p[v]]; del_set(p[v]); for(auto x : comp) { p[x] = p[u]; } comp.clear(); g[u].insert(v); g[v].insert(u); } void make_good(int v) { dfs(v); int par = p[v]; del_set(par); for(auto u : comp) { good[u] = 1; g[u].clear(); create(u); } for(auto u : comp) { assert(sz(g[u]) == 0); assert(siz[p[u]] == 1); } comp.clear(); } void solve() { int n, q; cin >> n >> q; for(int i = 1; i <= n; i++) idx.insert(i); for(int i = 1; i <= n; i++) create(i); string ans; while(q--) { char c; cin >> c; if(c == '+') { int u, v; cin >> u >> v; if(good[u]) make_good(v); else if(good[v]) make_good(u); else { if(p[u] == p[v]) { make_good(v); } else unite(u, v); } } else if(c == '-') { int v; cin >> v; if(good[v]) { assert(siz[p[v]] == 1); good[v] = 0; } else { int par = p[v]; assert(siz[par] != 1); siz[par]--; int prev = 0; for(auto to : g[v]) { if(prev) { g[prev].insert(to); g[to].insert(prev); } g[to].erase(v); prev = to; } g[v].clear(); create(v); } } else { int v; cin >> v; if(good[v]) ans.pb('1'); else if(siz[p[v]] == 1) ans.pb('0'); else ans.pb('?'); } } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } } |