#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) vector<ll> parent1,parent2, sz, computers; ll findElementHelper(ll kwadrat){ return (parent2[kwadrat] == kwadrat ? kwadrat : parent2[kwadrat] = findElementHelper(parent2[kwadrat])); } ll findElement(ll kolko){ ll kwadrat = parent1[kolko]; return findElementHelper(kwadrat); } void unionElements(ll kolko1, ll kolko2){ ll kwadrat1 = findElement(kolko1); ll kwadrat2 = findElement(kolko2); if(kwadrat1 == kwadrat2){ computers[kwadrat1]++; } else{ if(sz[kwadrat1] < sz[kwadrat2]) swap(kwadrat1, kwadrat2); parent2[kwadrat2] = kwadrat1; sz[kwadrat1] += sz[kwadrat2]; computers[kwadrat1] += computers[kwadrat2]+1;//bo w tym samym momencie dodajemy mozliwosc na komputer } } void eraseElement(ll kolko){ ll kwadrat = findElement(kolko); sz[kwadrat]--; computers[kwadrat]--; ll nowyKwadrat = parent2.size(); parent2.push_back(nowyKwadrat); sz.push_back(1); computers.push_back(0); parent1[kolko] = nowyKwadrat; } char answerQuery(ll kolko){ ll kwadrat = findElement(kolko); if(sz[kwadrat] == computers[kwadrat]){ return '1'; } else if(computers[kwadrat] == 0){ return '0'; } else{ return '?'; } } void solve() { ll n,q,a,b; char c; cin >> n >> q; parent1.resize(n+1); parent2.resize(n+1); sz.assign(n+1, 1); computers.assign(n+1, 0); for(ll i = 0; i <= n; i++){ parent1[i]=i; parent2[i]=i; } for(ll i = 0; i < q; i++){ cin >> c; if(c == '?'){ cin >> a; cout << answerQuery(a); } if(c == '+'){ cin >> a >> b; unionElements(a,b); } if(c == '-'){ cin >> a; eraseElement(a); } // cout << endl; // cout << "parent2\n"; // for(auto el : parent2) cout << el << " "; // cout << endl; // cout << "parent1\n"; // for(auto el : parent1) cout << el << " "; // cout << endl; // cout << "sz\n"; // for(auto el : sz) cout << el << " "; // cout << endl; // cout << "computers\n"; // for(auto el : computers) cout << el << " "; // cout << endl; // cout << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); }
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 114 115 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) vector<ll> parent1,parent2, sz, computers; ll findElementHelper(ll kwadrat){ return (parent2[kwadrat] == kwadrat ? kwadrat : parent2[kwadrat] = findElementHelper(parent2[kwadrat])); } ll findElement(ll kolko){ ll kwadrat = parent1[kolko]; return findElementHelper(kwadrat); } void unionElements(ll kolko1, ll kolko2){ ll kwadrat1 = findElement(kolko1); ll kwadrat2 = findElement(kolko2); if(kwadrat1 == kwadrat2){ computers[kwadrat1]++; } else{ if(sz[kwadrat1] < sz[kwadrat2]) swap(kwadrat1, kwadrat2); parent2[kwadrat2] = kwadrat1; sz[kwadrat1] += sz[kwadrat2]; computers[kwadrat1] += computers[kwadrat2]+1;//bo w tym samym momencie dodajemy mozliwosc na komputer } } void eraseElement(ll kolko){ ll kwadrat = findElement(kolko); sz[kwadrat]--; computers[kwadrat]--; ll nowyKwadrat = parent2.size(); parent2.push_back(nowyKwadrat); sz.push_back(1); computers.push_back(0); parent1[kolko] = nowyKwadrat; } char answerQuery(ll kolko){ ll kwadrat = findElement(kolko); if(sz[kwadrat] == computers[kwadrat]){ return '1'; } else if(computers[kwadrat] == 0){ return '0'; } else{ return '?'; } } void solve() { ll n,q,a,b; char c; cin >> n >> q; parent1.resize(n+1); parent2.resize(n+1); sz.assign(n+1, 1); computers.assign(n+1, 0); for(ll i = 0; i <= n; i++){ parent1[i]=i; parent2[i]=i; } for(ll i = 0; i < q; i++){ cin >> c; if(c == '?'){ cin >> a; cout << answerQuery(a); } if(c == '+'){ cin >> a >> b; unionElements(a,b); } if(c == '-'){ cin >> a; eraseElement(a); } // cout << endl; // cout << "parent2\n"; // for(auto el : parent2) cout << el << " "; // cout << endl; // cout << "parent1\n"; // for(auto el : parent1) cout << el << " "; // cout << endl; // cout << "sz\n"; // for(auto el : sz) cout << el << " "; // cout << endl; // cout << "computers\n"; // for(auto el : computers) cout << el << " "; // cout << endl; // cout << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); } |