#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
// const int INF = 1e18;
// const int mod = 1e9 + 7;
const int nax = 2e6;
int fu[nax], fu_size[nax], fu_removed[nax], fu_mapping[nax];
bool fu_full[nax];
int f(int x) {
if (fu[x] == x) {
return x;
}
return fu[x] = f(fu[x]);
}
void u(int x, int y) {
x = f(x);
y = f(y);
if (x == y) {
fu_full[x] = true;
} else {
fu_size[y] += fu_size[x];
fu_full[y] |= fu_full[x];
fu_removed[y] += fu_removed[x];
fu[x] = y;
}
}
int new_fu_vertex() {
static int fu_vertex = 0;
fu[fu_vertex] = fu_vertex;
fu_size[fu_vertex] = 1;
return fu_vertex++;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i <= n; i++) {
fu_mapping[i] = new_fu_vertex();
}
while (q--) {
char query_type;
int a, b, c, d;
cin >> query_type;
if (query_type == '+') {
cin >> a >> b;
u(fu_mapping[a], fu_mapping[b]);
} else if (query_type == '-') {
cin >> c;
fu_removed[f(fu_mapping[c])]++;
fu_mapping[c] = new_fu_vertex();
} else {
cin >> d;
d = f(fu_mapping[d]);
if (fu_full[d]) {
cout << 1;
} else if (fu_size[d] - fu_removed[d] == 1) {
cout << 0;
} else {
cout << '?';
}
}
}
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 | #include <bits/stdc++.h> using namespace std; // #define int long long // #define double long double // const int INF = 1e18; // const int mod = 1e9 + 7; const int nax = 2e6; int fu[nax], fu_size[nax], fu_removed[nax], fu_mapping[nax]; bool fu_full[nax]; int f(int x) { if (fu[x] == x) { return x; } return fu[x] = f(fu[x]); } void u(int x, int y) { x = f(x); y = f(y); if (x == y) { fu_full[x] = true; } else { fu_size[y] += fu_size[x]; fu_full[y] |= fu_full[x]; fu_removed[y] += fu_removed[x]; fu[x] = y; } } int new_fu_vertex() { static int fu_vertex = 0; fu[fu_vertex] = fu_vertex; fu_size[fu_vertex] = 1; return fu_vertex++; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; for (int i = 0; i <= n; i++) { fu_mapping[i] = new_fu_vertex(); } while (q--) { char query_type; int a, b, c, d; cin >> query_type; if (query_type == '+') { cin >> a >> b; u(fu_mapping[a], fu_mapping[b]); } else if (query_type == '-') { cin >> c; fu_removed[f(fu_mapping[c])]++; fu_mapping[c] = new_fu_vertex(); } else { cin >> d; d = f(fu_mapping[d]); if (fu_full[d]) { cout << 1; } else if (fu_size[d] - fu_removed[d] == 1) { cout << 0; } else { cout << '?'; } } } return 0; }/* */ |
English