#include <bits/stdc++.h>
using namespace std;
vector <int> rep;
vector <int> sizes;
vector <int> active;
vector <bool> sure;
vector <int> current_rep;
int new_count = 0;
int Find(int x) {
if (x == rep[x])
return x;
return Find(rep[x]);
}
void Union(int x, int y) {
x = Find(x);
y = Find(y);
if (sizes[x] < sizes[y]) {
swap(x,y);
}
if (x == y) {
sure[x] = true;
return;
}
sure[x] = sure[x] || sure[y];
sizes[x] += sizes[y];
active[x] += active[y];
rep[y] = x;
}
void create_node() {
rep.push_back(new_count);
sizes.push_back(1);
active.push_back(1);
sure.push_back(false);
new_count++;
}
void query(int x) {
x = current_rep[x];
x = Find(x);
if (sure[x])
cout << "1";
else if (active[x] <= 1) {
cout << "0";
}
else
cout << "?";
}
void del(int x) {
int stored = x;
x = current_rep[x];
x = Find(x);
active[x]--;
create_node();
current_rep[stored] = new_count - 1;
}
void add(int a, int b) {
a = current_rep[a];
b = current_rep[b];
Union(a,b);
}
int main() {
ios_base::sync_with_stdio(0);
int n, q;
cin >> n >> q;
// create_node();
// current_rep.push_back(0);
for(int i = 0; i <= n; i++) {
create_node();
current_rep.push_back(i);
}
for (int qq = 0; qq < q; qq++) {
string S;
cin >> S;
if (S == "?") {
int x;
cin >> x;
query(x);
}
else if (S == "-") {
int x;
cin >> x;
del(x);
}
else {
int a, b;
cin >> a >> b;
add(a, b);
}
}
cout << "\n";
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 | #include <bits/stdc++.h> using namespace std; vector <int> rep; vector <int> sizes; vector <int> active; vector <bool> sure; vector <int> current_rep; int new_count = 0; int Find(int x) { if (x == rep[x]) return x; return Find(rep[x]); } void Union(int x, int y) { x = Find(x); y = Find(y); if (sizes[x] < sizes[y]) { swap(x,y); } if (x == y) { sure[x] = true; return; } sure[x] = sure[x] || sure[y]; sizes[x] += sizes[y]; active[x] += active[y]; rep[y] = x; } void create_node() { rep.push_back(new_count); sizes.push_back(1); active.push_back(1); sure.push_back(false); new_count++; } void query(int x) { x = current_rep[x]; x = Find(x); if (sure[x]) cout << "1"; else if (active[x] <= 1) { cout << "0"; } else cout << "?"; } void del(int x) { int stored = x; x = current_rep[x]; x = Find(x); active[x]--; create_node(); current_rep[stored] = new_count - 1; } void add(int a, int b) { a = current_rep[a]; b = current_rep[b]; Union(a,b); } int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; // create_node(); // current_rep.push_back(0); for(int i = 0; i <= n; i++) { create_node(); current_rep.push_back(i); } for (int qq = 0; qq < q; qq++) { string S; cin >> S; if (S == "?") { int x; cin >> x; query(x); } else if (S == "-") { int x; cin >> x; del(x); } else { int a, b; cin >> a >> b; add(a, b); } } cout << "\n"; return 0; } |
English