#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
struct DSU {
vi G;
DSU(int n = 0) : G(n, -1) {}
int make() { G.pb(-1); return sz(G)-1; }
int find(int i) { return G[i] < 0 ? i : G[i] = find(G[i]); }
int join(int i, int j) {
if (G[i] > G[j]) swap(i, j);
G[i] += G[j]; G[j] = i;
return i;
}
};
void run() {
int n, q;
cin >> n >> q;
DSU dsu(n);
vi active(n, 1), cur(n);
iota(all(cur), 0);
while (q--) {
char type; cin >> type;
if (type == '+') {
int a, b;
cin >> a >> b;
a = dsu.find(cur[a-1]);
b = dsu.find(cur[b-1]);
if (a != b) {
int r = dsu.join(a, b);
if (active[a] < 0 || active[b] < 0) {
active[r] = -1;
} else {
active[r] = active[a] + active[b];
}
} else {
active[a] = -1;
}
} else if (type == '-') {
int c; cin >> c;
active[dsu.find(cur[c-1])]--;
cur[c-1] = dsu.make();
active.pb(1);
} else if (type == '?') {
int d; cin >> d;
d = dsu.find(cur[d-1]);
if (active[d] > 1) {
cout << '?';
} else {
cout << (active[d] < 0);
}
} else {
assert(0);
}
}
cout << '\n';
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(10);
run();
cout << flush; _Exit(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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) struct DSU { vi G; DSU(int n = 0) : G(n, -1) {} int make() { G.pb(-1); return sz(G)-1; } int find(int i) { return G[i] < 0 ? i : G[i] = find(G[i]); } int join(int i, int j) { if (G[i] > G[j]) swap(i, j); G[i] += G[j]; G[j] = i; return i; } }; void run() { int n, q; cin >> n >> q; DSU dsu(n); vi active(n, 1), cur(n); iota(all(cur), 0); while (q--) { char type; cin >> type; if (type == '+') { int a, b; cin >> a >> b; a = dsu.find(cur[a-1]); b = dsu.find(cur[b-1]); if (a != b) { int r = dsu.join(a, b); if (active[a] < 0 || active[b] < 0) { active[r] = -1; } else { active[r] = active[a] + active[b]; } } else { active[a] = -1; } } else if (type == '-') { int c; cin >> c; active[dsu.find(cur[c-1])]--; cur[c-1] = dsu.make(); active.pb(1); } else if (type == '?') { int d; cin >> d; d = dsu.find(cur[d-1]); if (active[d] > 1) { cout << '?'; } else { cout << (active[d] < 0); } } else { assert(0); } } cout << '\n'; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |
English