#include<iostream> #include<unordered_set> #include<vector> using namespace std; const int max_n = 3e5; int n, q, set_id[max_n], computer[max_n]{0}, in_set[max_n]{0}; vector<int>id[max_n]; unordered_set<int>ava; void connect(int a, int b){ int s_a = set_id[a], s_b = set_id[b]; if(in_set[s_a] > in_set[s_b]){//b->a computer[s_a] += computer[s_b]+1; computer[s_b] = 0; in_set[s_a] += in_set[s_b]; in_set[s_b] = 0; ava.insert(s_b); int s = id[s_b].size(); for(int i=s-1; i>=0; i--){ if(set_id[id[s_b][i]] == s_b){ set_id[id[s_b][i]] = s_a; id[s_a].push_back(id[s_b][i]); } id[s_b].pop_back(); } } else{ computer[s_b] += computer[s_a]+1; computer[s_a] = 0; in_set[s_b] += in_set[s_a]; in_set[s_a] = 0; ava.insert(s_a); int s = id[s_a].size(); for(int i=s-1; i>=0; i--){ if(set_id[id[s_a][i]] == s_a){ set_id[id[s_a][i]] = s_b; id[s_b].push_back(id[s_a][i]); } id[s_a].pop_back(); } } } void del(int a){ int s_a = set_id[a], new_s_a = *ava.begin(); ava.erase(new_s_a); computer[s_a]--; in_set[s_a]--; set_id[a] = new_s_a; computer[new_s_a]=0; in_set[new_s_a]=1; id[new_s_a].push_back(a); } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>q; for(int i=0; i<n; i++){ set_id[i] = i; in_set[i] = 1; id[i].push_back(i); } int a, b; char x; for(int i=0; i<q; i++){ cin>>x>>a; --a; if(x == '?'){ if(computer[set_id[a]] >= in_set[set_id[a]])cout<<'1'; else if(computer[set_id[a]] == 0)cout<<'0'; else cout<<'?'; } else if(x == '+'){ cin>>b; --b; if(set_id[a] != set_id[b])connect(a,b); else computer[set_id[a]]++; } else{ del(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 | #include<iostream> #include<unordered_set> #include<vector> using namespace std; const int max_n = 3e5; int n, q, set_id[max_n], computer[max_n]{0}, in_set[max_n]{0}; vector<int>id[max_n]; unordered_set<int>ava; void connect(int a, int b){ int s_a = set_id[a], s_b = set_id[b]; if(in_set[s_a] > in_set[s_b]){//b->a computer[s_a] += computer[s_b]+1; computer[s_b] = 0; in_set[s_a] += in_set[s_b]; in_set[s_b] = 0; ava.insert(s_b); int s = id[s_b].size(); for(int i=s-1; i>=0; i--){ if(set_id[id[s_b][i]] == s_b){ set_id[id[s_b][i]] = s_a; id[s_a].push_back(id[s_b][i]); } id[s_b].pop_back(); } } else{ computer[s_b] += computer[s_a]+1; computer[s_a] = 0; in_set[s_b] += in_set[s_a]; in_set[s_a] = 0; ava.insert(s_a); int s = id[s_a].size(); for(int i=s-1; i>=0; i--){ if(set_id[id[s_a][i]] == s_a){ set_id[id[s_a][i]] = s_b; id[s_b].push_back(id[s_a][i]); } id[s_a].pop_back(); } } } void del(int a){ int s_a = set_id[a], new_s_a = *ava.begin(); ava.erase(new_s_a); computer[s_a]--; in_set[s_a]--; set_id[a] = new_s_a; computer[new_s_a]=0; in_set[new_s_a]=1; id[new_s_a].push_back(a); } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>q; for(int i=0; i<n; i++){ set_id[i] = i; in_set[i] = 1; id[i].push_back(i); } int a, b; char x; for(int i=0; i<q; i++){ cin>>x>>a; --a; if(x == '?'){ if(computer[set_id[a]] >= in_set[set_id[a]])cout<<'1'; else if(computer[set_id[a]] == 0)cout<<'0'; else cout<<'?'; } else if(x == '+'){ cin>>b; --b; if(set_id[a] != set_id[b])connect(a,b); else computer[set_id[a]]++; } else{ del(a); } } return 0; } |