#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 const int INF = 1e9; void get(int &a) { a = 0; int g = getchar_unlocked(); while (g < '0' || '9' < g) g = getchar_unlocked(); while ('0' <= g && g <= '9') { a = (a << 3) + (a << 1) + g - '0'; g = getchar_unlocked(); } } void get_type(char &c) { c = getchar_unlocked(); while (c != '+' && c != '-' && c != '?') c = getchar_unlocked(); } int main() { int n, q; get(n); get(q); vector<int> current_id(n); iota(current_id.begin(), current_id.end(), 0); vector<vector<int>> graph(n + q); vector<int> state(n + q, 0); vector<int> rep(n + q), roz(n + q, 1), deg(n + q), cycle_vis(n + q); iota(rep.begin(), rep.end(), 0); function<int(int)> fin = [&](int v) { if (v != rep[v]) rep[v] = fin(rep[v]); return rep[v]; }; auto uni = [&](int u, int v) { u = fin(u); v = fin(v); assert(u != v); if (roz[u] < roz[v]) swap(u, v); roz[u] += roz[v]; rep[v] = u; }; int licz = n; REP (i, q) { char c; get_type(c); if (c == '?') { int v; get(v); --v; v = current_id[v]; if (state[v] == -1) putchar_unlocked('?'); else if (state[v] == 0) putchar_unlocked('0'); else putchar_unlocked('1'); } else if (c == '+') { int u, v; get(u); get(v); --u; --v; u = current_id[u]; v = current_id[v]; graph[u].emplace_back(v); graph[v].emplace_back(u); if (state[u] == 0) state[u] = -1; if (state[v] == 0) state[v] = -1; function<void(int)> dfs = [&](int x) { state[x] = 1; cycle_vis[x] = 1; for (int o : graph[x]) if (!cycle_vis[o]) dfs(o); }; if (fin(u) == fin(v) || cycle_vis[u] || cycle_vis[v]) { if (!cycle_vis[u]) dfs(u); if (!cycle_vis[v]) dfs(v); } else { ++deg[u]; ++deg[v]; uni(u, v); } } else { int v; get(v); --v; int old_id = current_id[v]; current_id[v] = licz++; v = old_id; if (cycle_vis[v]) continue; state[v] = 1; if (deg[v] == 1) { function<void(int)> dfs = [&](int x) { deg[x] = -INF; for (int o : graph[x]) { if (deg[o] > 0) { --deg[x]; --deg[o]; if (!deg[o]) state[o] = 0; else if (deg[o] == 1 && state[o] == 1) dfs(o); } } }; dfs(v); } } } putchar_unlocked('\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 | #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 const int INF = 1e9; void get(int &a) { a = 0; int g = getchar_unlocked(); while (g < '0' || '9' < g) g = getchar_unlocked(); while ('0' <= g && g <= '9') { a = (a << 3) + (a << 1) + g - '0'; g = getchar_unlocked(); } } void get_type(char &c) { c = getchar_unlocked(); while (c != '+' && c != '-' && c != '?') c = getchar_unlocked(); } int main() { int n, q; get(n); get(q); vector<int> current_id(n); iota(current_id.begin(), current_id.end(), 0); vector<vector<int>> graph(n + q); vector<int> state(n + q, 0); vector<int> rep(n + q), roz(n + q, 1), deg(n + q), cycle_vis(n + q); iota(rep.begin(), rep.end(), 0); function<int(int)> fin = [&](int v) { if (v != rep[v]) rep[v] = fin(rep[v]); return rep[v]; }; auto uni = [&](int u, int v) { u = fin(u); v = fin(v); assert(u != v); if (roz[u] < roz[v]) swap(u, v); roz[u] += roz[v]; rep[v] = u; }; int licz = n; REP (i, q) { char c; get_type(c); if (c == '?') { int v; get(v); --v; v = current_id[v]; if (state[v] == -1) putchar_unlocked('?'); else if (state[v] == 0) putchar_unlocked('0'); else putchar_unlocked('1'); } else if (c == '+') { int u, v; get(u); get(v); --u; --v; u = current_id[u]; v = current_id[v]; graph[u].emplace_back(v); graph[v].emplace_back(u); if (state[u] == 0) state[u] = -1; if (state[v] == 0) state[v] = -1; function<void(int)> dfs = [&](int x) { state[x] = 1; cycle_vis[x] = 1; for (int o : graph[x]) if (!cycle_vis[o]) dfs(o); }; if (fin(u) == fin(v) || cycle_vis[u] || cycle_vis[v]) { if (!cycle_vis[u]) dfs(u); if (!cycle_vis[v]) dfs(v); } else { ++deg[u]; ++deg[v]; uni(u, v); } } else { int v; get(v); --v; int old_id = current_id[v]; current_id[v] = licz++; v = old_id; if (cycle_vis[v]) continue; state[v] = 1; if (deg[v] == 1) { function<void(int)> dfs = [&](int x) { deg[x] = -INF; for (int o : graph[x]) { if (deg[o] > 0) { --deg[x]; --deg[o]; if (!deg[o]) state[o] = 0; else if (deg[o] == 1 && state[o] == 1) dfs(o); } } }; dfs(v); } } } putchar_unlocked('\n'); } |