#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; // actual code #define NMAX 1500005 int n, m, q, p[NMAX], s[NMAX], c[NMAX], rc[NMAX], mem[NMAX]; int parent(int v) { int w = v; while (p[v] != v) v = p[v]; while (w != v) { int x = p[w]; p[w] = v; w = x; } return v; } void merge(int u, int v) { prd("merge %d %d\n", u, v); u = parent(u); v = parent(v); prd("parents %d %d\n", u, v); if (u == v) { s[u] = 2; return; } if (c[u] > c[v]) { int x = u; u = v; v = x; } // u ---> v p[u] = v; c[v] += c[u]; rc[v] += rc[u]; int status = max(s[u], s[v]); if (status > 0) s[v] = status; else s[v] = 1; } void remove(int v) { int u = parent(mem[v]); rc[u]--; mem[v] = ++m; p[m] = m; c[m] = rc[m] = 1; s[m] = 0; prd("remove %d -> %d\n", v, m); } void query(int v) { prd("query: "); v = parent(v); if (s[v]==0) printf("0"); if (s[v]==1) { if (rc[v] > 1) printf("?"); else printf("0"); } if (s[v]==2) printf("1"); prd("\n"); } int main() { scanf("%d%d", &n, &q); m = n; FORE(i, 1, n) { p[i] = i; mem[i] = i; c[i] = rc[i] = 1; s[i] = 0; } REP(i, q) { int u, v; char ch; scanf(" %c", &ch); prd("%c\n", ch); if (ch =='+') { scanf("%d%d", &u, &v); merge(mem[u], mem[v]); } if (ch =='-') { scanf("%d", &v); remove(v); } if (ch =='?') { scanf("%d", &v); query(mem[v]); } } return 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 144 145 146 147 148 149 150 151 152 153 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; // actual code #define NMAX 1500005 int n, m, q, p[NMAX], s[NMAX], c[NMAX], rc[NMAX], mem[NMAX]; int parent(int v) { int w = v; while (p[v] != v) v = p[v]; while (w != v) { int x = p[w]; p[w] = v; w = x; } return v; } void merge(int u, int v) { prd("merge %d %d\n", u, v); u = parent(u); v = parent(v); prd("parents %d %d\n", u, v); if (u == v) { s[u] = 2; return; } if (c[u] > c[v]) { int x = u; u = v; v = x; } // u ---> v p[u] = v; c[v] += c[u]; rc[v] += rc[u]; int status = max(s[u], s[v]); if (status > 0) s[v] = status; else s[v] = 1; } void remove(int v) { int u = parent(mem[v]); rc[u]--; mem[v] = ++m; p[m] = m; c[m] = rc[m] = 1; s[m] = 0; prd("remove %d -> %d\n", v, m); } void query(int v) { prd("query: "); v = parent(v); if (s[v]==0) printf("0"); if (s[v]==1) { if (rc[v] > 1) printf("?"); else printf("0"); } if (s[v]==2) printf("1"); prd("\n"); } int main() { scanf("%d%d", &n, &q); m = n; FORE(i, 1, n) { p[i] = i; mem[i] = i; c[i] = rc[i] = 1; s[i] = 0; } REP(i, q) { int u, v; char ch; scanf(" %c", &ch); prd("%c\n", ch); if (ch =='+') { scanf("%d%d", &u, &v); merge(mem[u], mem[v]); } if (ch =='-') { scanf("%d", &v); remove(v); } if (ch =='?') { scanf("%d", &v); query(mem[v]); } } return 0; } |