// #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 1e6; int a, b, fa[N + 5], ans[N + 5], top; bool vis[N + 5]; struct node { int c[2], fa, r; } s[N + 5]; struct pii { int x, y; }; vector<pii> st[N + 5]; #define ls s[n].c[0] #define rs s[n].c[1] bool get(int n) {return s[s[n].fa].c[1] == n;} bool is(int n) {return s[s[n].fa].c[get(n)] != n;} void up(int n) {} void upd(int n) {swap(ls, rs); s[n].r ^= 1;} void dw(int n) {if(!s[n].r) return; upd(ls), upd(rs); s[n].r = 0;} void ps(int n) {if(!is(n)) ps(s[n].fa); dw(n);} void con(int x, int y, int z) {if(x) s[x].c[z] = y; if(y) s[y].fa = x;} void rot(int n) { int f = s[n].fa, ff = s[f].fa, p = get(n); if(!is(f)) s[ff].c[get(f)] = n; s[n].fa = ff; con(f, s[n].c[p ^ 1], p); con(n, f, p ^ 1); up(f), up(n); } void spl(int n) { ps(n); for(; !is(n); rot(n)) if(!is(s[n].fa)) rot(get(s[n].fa) == get(n) ? s[n].fa : n); } void acc(int n) {for(int x = 0; n; x = n, n = s[n].fa) spl(n), s[n].c[1] = x, up(n);} void mkr(int n) {acc(n), spl(n), upd(n);} void slt(int n, int m) {mkr(m), acc(n), spl(n);} int fnd(int n) {acc(n), spl(n); for(dw(n); ls; n = ls, dw(n)); return n;} void lk(int n, int m) {mkr(n); if(fnd(m) != n) s[n].fa = m;} bool cut(int n, int m) {slt(n, m); if(ls == m && !s[m].c[1]) return s[m].fa = ls = 0, up(n), 1; return 0;} bool ck(int n) { while(siz(st[n])) { if(vis[st[n].back().y]) st[n].pop_back(); else return 0; } return 1; } void dfs(int n) { if(ans[n] == 1) return; ans[n] = 1; while(siz(st[n])) { auto [x, y] = st[n].back(); st[n].pop_back(); if(vis[y]) continue; vis[y] = 1; cut(n, x); // cerr << x << ' ' << y << endl; dfs(x); } } signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b; rep(i, 1, a) fa[i] = i; char o; int x, y; rep(i, 1, b) { cin >> o >> x; if(o == '?') { if(ans[x] == -1) cout << '?'; else cout << ans[x]; } else if(o == '+') { cin >> y; // cerr << x << ' ' << y << endl; if(x == y) { dfs(x); continue; } // cerr << i << ' ' << x << ' ' << y << ' ' << ans[x] << ' ' << ans[y] << endl; mkr(x); if(fnd(y) == x) { // cerr << i << ' ' << x << endl; dfs(x); } else { // cerr << i << ' ' << ans[x] << ' ' << ans[y] << endl; if(ans[x] == 1 && ans[y] == 1) assert(0); if(ans[x] == 1 || ans[y] == 1) { dfs(x), dfs(y); continue; } ++top; st[x].PB(pii{y, top}), st[y].PB(pii{x, top}); lk(x, y); ans[x] = ans[y] = -1; } // cerr << "#@#@!@"; // rep(i, 1, a) cerr << ans[i] << " \n"[i == a]; } else { // cerr << x << endl; if(ans[x] == -1) { vi tmp; for(auto [v, id] : st[x]) { vis[id] = 1; if(cut(x, v)) tmp.PB(v); } rep(i, 0, siz(tmp) - 2) { int x = tmp[i], y = tmp[i + 1]; ++top; st[x].PB(pii{y, top}), st[y].PB(pii{x, top}); lk(x, y); } // cerr << siz(tmp) << ' ' << ck(tmp[0]) << ' ' << tmp[0] << endl; if(siz(tmp) == 1 && ck(tmp[0])) ans[tmp[0]] = 0; } ans[x] = 0; // cerr << "#@#@!@"; // rep(i, 1, a) cerr << ans[i] << " \n"[i == a]; } } return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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 136 137 138 139 140 141 142 143 | // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 1e6; int a, b, fa[N + 5], ans[N + 5], top; bool vis[N + 5]; struct node { int c[2], fa, r; } s[N + 5]; struct pii { int x, y; }; vector<pii> st[N + 5]; #define ls s[n].c[0] #define rs s[n].c[1] bool get(int n) {return s[s[n].fa].c[1] == n;} bool is(int n) {return s[s[n].fa].c[get(n)] != n;} void up(int n) {} void upd(int n) {swap(ls, rs); s[n].r ^= 1;} void dw(int n) {if(!s[n].r) return; upd(ls), upd(rs); s[n].r = 0;} void ps(int n) {if(!is(n)) ps(s[n].fa); dw(n);} void con(int x, int y, int z) {if(x) s[x].c[z] = y; if(y) s[y].fa = x;} void rot(int n) { int f = s[n].fa, ff = s[f].fa, p = get(n); if(!is(f)) s[ff].c[get(f)] = n; s[n].fa = ff; con(f, s[n].c[p ^ 1], p); con(n, f, p ^ 1); up(f), up(n); } void spl(int n) { ps(n); for(; !is(n); rot(n)) if(!is(s[n].fa)) rot(get(s[n].fa) == get(n) ? s[n].fa : n); } void acc(int n) {for(int x = 0; n; x = n, n = s[n].fa) spl(n), s[n].c[1] = x, up(n);} void mkr(int n) {acc(n), spl(n), upd(n);} void slt(int n, int m) {mkr(m), acc(n), spl(n);} int fnd(int n) {acc(n), spl(n); for(dw(n); ls; n = ls, dw(n)); return n;} void lk(int n, int m) {mkr(n); if(fnd(m) != n) s[n].fa = m;} bool cut(int n, int m) {slt(n, m); if(ls == m && !s[m].c[1]) return s[m].fa = ls = 0, up(n), 1; return 0;} bool ck(int n) { while(siz(st[n])) { if(vis[st[n].back().y]) st[n].pop_back(); else return 0; } return 1; } void dfs(int n) { if(ans[n] == 1) return; ans[n] = 1; while(siz(st[n])) { auto [x, y] = st[n].back(); st[n].pop_back(); if(vis[y]) continue; vis[y] = 1; cut(n, x); // cerr << x << ' ' << y << endl; dfs(x); } } signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b; rep(i, 1, a) fa[i] = i; char o; int x, y; rep(i, 1, b) { cin >> o >> x; if(o == '?') { if(ans[x] == -1) cout << '?'; else cout << ans[x]; } else if(o == '+') { cin >> y; // cerr << x << ' ' << y << endl; if(x == y) { dfs(x); continue; } // cerr << i << ' ' << x << ' ' << y << ' ' << ans[x] << ' ' << ans[y] << endl; mkr(x); if(fnd(y) == x) { // cerr << i << ' ' << x << endl; dfs(x); } else { // cerr << i << ' ' << ans[x] << ' ' << ans[y] << endl; if(ans[x] == 1 && ans[y] == 1) assert(0); if(ans[x] == 1 || ans[y] == 1) { dfs(x), dfs(y); continue; } ++top; st[x].PB(pii{y, top}), st[y].PB(pii{x, top}); lk(x, y); ans[x] = ans[y] = -1; } // cerr << "#@#@!@"; // rep(i, 1, a) cerr << ans[i] << " \n"[i == a]; } else { // cerr << x << endl; if(ans[x] == -1) { vi tmp; for(auto [v, id] : st[x]) { vis[id] = 1; if(cut(x, v)) tmp.PB(v); } rep(i, 0, siz(tmp) - 2) { int x = tmp[i], y = tmp[i + 1]; ++top; st[x].PB(pii{y, top}), st[y].PB(pii{x, top}); lk(x, y); } // cerr << siz(tmp) << ' ' << ck(tmp[0]) << ' ' << tmp[0] << endl; if(siz(tmp) == 1 && ck(tmp[0])) ans[tmp[0]] = 0; } ans[x] = 0; // cerr << "#@#@!@"; // rep(i, 1, a) cerr << ans[i] << " \n"[i == a]; } } return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0; } |