#define first f #define second s #include <bits/stdc++.h> using namespace std; int n , q , a , b , r , it = 300001; char c; int wie[1300005]; int pol[1300005]; int rep[300005]; set<int> zbi[1300005]; void polacz(int a , int b) { if(a == b) { pol[a] ++; return; } if(wie[b] > wie[a]) swap(a , b); //cout << a << b << " "; for(auto i : zbi[b]) { //cout << i; zbi[a].insert(i); rep[i] = a; } //cout << "\n"; pol[a] += pol[b] + 1; wie[a] += wie[b]; pol[b] = 0; wie[b] = 0; zbi[b].clear(); } void usun(int a) { wie[rep[a]] --; pol[rep[a]] --; /*for(auto i : zbi[rep[a]]) { cout << i << " "; } cout << "\n";*/ zbi[rep[a]].erase(zbi[rep[a]].lower_bound(a)); rep[a] = it; wie[it] ++; zbi[it].insert(a); it ++; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for(int i = 1 ; i <= n ; i ++) { wie[i] = 1; rep[i] = i; zbi[i].insert(i); } for(int i = 1 ; i <= q ; i ++) { cin >> c; if(c == '+') { cin >> a >> b; polacz(rep[a] , rep[b]); } if(c == '?') { cin >> a; r = rep[a]; if(pol[r] >= wie[r]) cout << 1; else if(pol[r] == 0) cout << 0; else cout << "?"; } if(c == '-') { cin >> a; usun(a); } } 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 | #define first f #define second s #include <bits/stdc++.h> using namespace std; int n , q , a , b , r , it = 300001; char c; int wie[1300005]; int pol[1300005]; int rep[300005]; set<int> zbi[1300005]; void polacz(int a , int b) { if(a == b) { pol[a] ++; return; } if(wie[b] > wie[a]) swap(a , b); //cout << a << b << " "; for(auto i : zbi[b]) { //cout << i; zbi[a].insert(i); rep[i] = a; } //cout << "\n"; pol[a] += pol[b] + 1; wie[a] += wie[b]; pol[b] = 0; wie[b] = 0; zbi[b].clear(); } void usun(int a) { wie[rep[a]] --; pol[rep[a]] --; /*for(auto i : zbi[rep[a]]) { cout << i << " "; } cout << "\n";*/ zbi[rep[a]].erase(zbi[rep[a]].lower_bound(a)); rep[a] = it; wie[it] ++; zbi[it].insert(a); it ++; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for(int i = 1 ; i <= n ; i ++) { wie[i] = 1; rep[i] = i; zbi[i].insert(i); } for(int i = 1 ; i <= q ; i ++) { cin >> c; if(c == '+') { cin >> a >> b; polacz(rep[a] , rep[b]); } if(c == '?') { cin >> a; r = rep[a]; if(pol[r] >= wie[r]) cout << 1; else if(pol[r] == 0) cout << 0; else cout << "?"; } if(c == '-') { cin >> a; usun(a); } } return 0; } |