#include <cstdio> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) #define STR(n,x) char x[n]; scanf("%s", x) typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VII; bool solo[300000], has[300000]; vector<VII> link; vector<vector<VII> > blink; vector<VI> life; PII& getLink(PII v) { return link[v.FT][v.SD]; } vector<PII>& getBlink(PII v) { return blink[v.FT][v.SD]; } int& getLife(PII v) { return life[v.FT][v.SD]; } PII find(PII v) { PII r = v; while (getLink(r) != r) r = getLink(r); while (getLink(v) != r) { PII w = getLink(v); getLink(v) = r; getBlink(r).PB(v); v = w; } return r; } PII find(int v) { return find(MP(v, SIZE(link[v]) - 1)); } void unsolo(int v) { if (!solo[v]) return; link[v].PB(MP(v, SIZE(link[v]))); blink[v].PB(VII()); life[v].PB(1); solo[v] = 0; } void kill(PII v, bool x) { if (!solo[v.FT] && v.SD == SIZE(link[v.FT]) - 1) { has[v.FT] = x; solo[v.FT] = 1; if (!x) return; } vector<PII>& b = getBlink(v); FORE(it,b) if (getLink(*it) == v) kill(*it, x); } void add(int v, int w) { if (has[v]) v = w; if (has[w]) w = v; if (v == w && solo[v]) { has[v] = 1; return; } unsolo(v); unsolo(w); PII pv = find(v), pw = find(w); if (pv == pw) kill(pv, 1); else { getLink(pv) = pw; getBlink(pw).PB(pv); getLife(pw) += getLife(pv); } } void take(int v) { if (solo[v]) { has[v] = 0; return; } has[v] = 0; solo[v] = 1; PII pv = find(v); if (--getLife(pv) == 1) kill(pv, 0); } char check(int v) { if (has[v]) return '1'; if (solo[v]) return '0'; return '?'; } int main() { INT(n); INT(q); REP(i,n) solo[i] = 1; link.resize(n); blink.resize(n); life.resize(n); REP(qq,q) { STR(10, op); switch (op[0]) { case '+': { INT(a); INT(b); --a; --b; add(a, b); break; } case '-': { INT(c); --c; take(c); break; } default: { INT(d); --d; putchar(check(d)); break; } } } putchar('\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 140 141 | #include <cstdio> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) #define STR(n,x) char x[n]; scanf("%s", x) typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VII; bool solo[300000], has[300000]; vector<VII> link; vector<vector<VII> > blink; vector<VI> life; PII& getLink(PII v) { return link[v.FT][v.SD]; } vector<PII>& getBlink(PII v) { return blink[v.FT][v.SD]; } int& getLife(PII v) { return life[v.FT][v.SD]; } PII find(PII v) { PII r = v; while (getLink(r) != r) r = getLink(r); while (getLink(v) != r) { PII w = getLink(v); getLink(v) = r; getBlink(r).PB(v); v = w; } return r; } PII find(int v) { return find(MP(v, SIZE(link[v]) - 1)); } void unsolo(int v) { if (!solo[v]) return; link[v].PB(MP(v, SIZE(link[v]))); blink[v].PB(VII()); life[v].PB(1); solo[v] = 0; } void kill(PII v, bool x) { if (!solo[v.FT] && v.SD == SIZE(link[v.FT]) - 1) { has[v.FT] = x; solo[v.FT] = 1; if (!x) return; } vector<PII>& b = getBlink(v); FORE(it,b) if (getLink(*it) == v) kill(*it, x); } void add(int v, int w) { if (has[v]) v = w; if (has[w]) w = v; if (v == w && solo[v]) { has[v] = 1; return; } unsolo(v); unsolo(w); PII pv = find(v), pw = find(w); if (pv == pw) kill(pv, 1); else { getLink(pv) = pw; getBlink(pw).PB(pv); getLife(pw) += getLife(pv); } } void take(int v) { if (solo[v]) { has[v] = 0; return; } has[v] = 0; solo[v] = 1; PII pv = find(v); if (--getLife(pv) == 1) kill(pv, 0); } char check(int v) { if (has[v]) return '1'; if (solo[v]) return '0'; return '?'; } int main() { INT(n); INT(q); REP(i,n) solo[i] = 1; link.resize(n); blink.resize(n); life.resize(n); REP(qq,q) { STR(10, op); switch (op[0]) { case '+': { INT(a); INT(b); --a; --b; add(a, b); break; } case '-': { INT(c); --c; take(c); break; } default: { INT(d); --d; putchar(check(d)); break; } } } putchar('\n'); } |