// clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif // clang-format on // Global variables int nvils, nqueries; vector<int> vil_to_dsu; vector<int> parent, dsu_rank; vector<int> ncomps, active_cnt; int find_leader(const int& v); int vil_to_dsu_ensure(int vil); void connect(int vila, int vilb); int main() { cin.tie(0)->sync_with_stdio(0); cin >> nvils >> nqueries; vil_to_dsu.resize(nvils + 1, -1); parent.reserve(nqueries); dsu_rank.reserve(nqueries); ncomps.reserve(nqueries); active_cnt.reserve(nqueries); // Process query while (nqueries--) { char t; int a, b; cin >> t >> a; // Ask if (t == '?') { int l = find_leader(vil_to_dsu_ensure(a)); if (ncomps[l] == 0) cout << "0"; else if (ncomps[l] >= active_cnt[l]) cout << "1"; else cout << "?"; continue; } // Separate a vil from his group if (t == '-') { if (vil_to_dsu[a] == -1) continue; int l = find_leader(vil_to_dsu[a]); active_cnt[l] -= 1; ncomps[l] -= 1; vil_to_dsu[a] = -1; continue; } // Add a computer to the system cin >> b; connect(a, b); } cout << "\n"; return 0; } void connect(int vila, int vilb) { int a = vil_to_dsu_ensure(vila), b = vil_to_dsu_ensure(vilb); a = find_leader(a), b = find_leader(b); if (a == b) { ncomps[a] += 1; return; } if (dsu_rank[a] > dsu_rank[b]) swap(a, b); // to join a to b parent[a] = b; dsu_rank[b] += dsu_rank[a]; active_cnt[b] += active_cnt[a]; ncomps[b] += ncomps[a] + 1; } int find_leader(const int& v) { if (parent[v] == v) return v; return parent[v] = find_leader(parent[v]); } int vil_to_dsu_ensure(int vil) { auto& v = vil_to_dsu[vil]; if (v == -1) { v = ssize(parent); parent.emplace_back(v); dsu_rank.emplace_back(1); active_cnt.emplace_back(1); ncomps.emplace_back(0); } return v; }
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 109 110 111 112 113 | // clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif // clang-format on // Global variables int nvils, nqueries; vector<int> vil_to_dsu; vector<int> parent, dsu_rank; vector<int> ncomps, active_cnt; int find_leader(const int& v); int vil_to_dsu_ensure(int vil); void connect(int vila, int vilb); int main() { cin.tie(0)->sync_with_stdio(0); cin >> nvils >> nqueries; vil_to_dsu.resize(nvils + 1, -1); parent.reserve(nqueries); dsu_rank.reserve(nqueries); ncomps.reserve(nqueries); active_cnt.reserve(nqueries); // Process query while (nqueries--) { char t; int a, b; cin >> t >> a; // Ask if (t == '?') { int l = find_leader(vil_to_dsu_ensure(a)); if (ncomps[l] == 0) cout << "0"; else if (ncomps[l] >= active_cnt[l]) cout << "1"; else cout << "?"; continue; } // Separate a vil from his group if (t == '-') { if (vil_to_dsu[a] == -1) continue; int l = find_leader(vil_to_dsu[a]); active_cnt[l] -= 1; ncomps[l] -= 1; vil_to_dsu[a] = -1; continue; } // Add a computer to the system cin >> b; connect(a, b); } cout << "\n"; return 0; } void connect(int vila, int vilb) { int a = vil_to_dsu_ensure(vila), b = vil_to_dsu_ensure(vilb); a = find_leader(a), b = find_leader(b); if (a == b) { ncomps[a] += 1; return; } if (dsu_rank[a] > dsu_rank[b]) swap(a, b); // to join a to b parent[a] = b; dsu_rank[b] += dsu_rank[a]; active_cnt[b] += active_cnt[a]; ncomps[b] += ncomps[a] + 1; } int find_leader(const int& v) { if (parent[v] == v) return v; return parent[v] = find_leader(parent[v]); } int vil_to_dsu_ensure(int vil) { auto& v = vil_to_dsu[vil]; if (v == -1) { v = ssize(parent); parent.emplace_back(v); dsu_rank.emplace_back(1); active_cnt.emplace_back(1); ncomps.emplace_back(0); } return v; } |