#include <iostream> #include <map> #include <vector> using namespace std; constexpr int maxn = 1'300'300; int ppl; int rep[maxn]; int edge[maxn]; int vert[maxn]; int trans[maxn]; int fin(int a) { if (rep[a] == a) return a; return rep[a] = fin(rep[a]); } void uni(int a, int b) { a = fin(a); b = fin(b); if (a == b) edge[a]++; else { rep[a] = b; edge[b] += edge[a] + 1; vert[b] += vert[a]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; ppl = n; for(int i = 0; i < maxn; i++) { rep[i] = i; vert[i] = 1; edge[i] = 0; trans[i] = i; } string s; vector<char> ans; int a,b; for(int i = 0; i < q; i++) { cin >> s >> a; if (s == "?") { a = fin(trans[a]); if (vert[a] == edge[a]) { ans.push_back('1'); continue; } if (vert[a] == 1) { ans.push_back('0'); continue; } ans.push_back('?'); continue; } if (s == "-") { b = fin(trans[a]); trans[a] = ++ppl; vert[b]--; edge[b]--; continue; } cin >> b; uni(trans[a], trans[b]); } cout << string(ans.begin(), ans.end()) << "\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 | #include <iostream> #include <map> #include <vector> using namespace std; constexpr int maxn = 1'300'300; int ppl; int rep[maxn]; int edge[maxn]; int vert[maxn]; int trans[maxn]; int fin(int a) { if (rep[a] == a) return a; return rep[a] = fin(rep[a]); } void uni(int a, int b) { a = fin(a); b = fin(b); if (a == b) edge[a]++; else { rep[a] = b; edge[b] += edge[a] + 1; vert[b] += vert[a]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; ppl = n; for(int i = 0; i < maxn; i++) { rep[i] = i; vert[i] = 1; edge[i] = 0; trans[i] = i; } string s; vector<char> ans; int a,b; for(int i = 0; i < q; i++) { cin >> s >> a; if (s == "?") { a = fin(trans[a]); if (vert[a] == edge[a]) { ans.push_back('1'); continue; } if (vert[a] == 1) { ans.push_back('0'); continue; } ans.push_back('?'); continue; } if (s == "-") { b = fin(trans[a]); trans[a] = ++ppl; vert[b]--; edge[b]--; continue; } cin >> b; uni(trans[a], trans[b]); } cout << string(ans.begin(), ans.end()) << "\n"; } |