#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ; #define debug(x) cout << "[" << #x << " " << x << "] "; #define ar array #define ll long long #define ld long double #define sz(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() typedef vector<int> vi; typedef pair<int,int> pi; const int MAX_N = 3e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; char state[MAX_N]; // vi graph[MAX_N]; struct DSU{ int parent[MAX_N]; int size[MAX_N]; DSU(){ for(int i=0; i<MAX_N; i++){ parent[i] = i; size[i] = 1; } } int find(int x){ if(parent[x] == x)return x; return parent[x] = find(parent[x]); } void merge(int a, int b){ a = find(a); b = find(b); if(a == b)return; if(size[a] < size[b])swap(a,b); parent[b] = a; size[a] += size[b]; } }; int n; DSU dsu = DSU(); // void dfs1(int u, int p = -1){ // for(int v: graph[u]){ // if(state[v] != '?')continue; // // if(state[v] == '?'){ // // state[v] = '1'; // // } // state[v] = '1'; // dsu.parent[v] = v; // dsu.size[v] = 1; // dfs1(v,u); // } // graph[u].clear(); // } // void dfs2(int u, int p = -1){ // if(p == -1){ // if(graph[u].size() == 1){ // dfs2(graph[u][0],u); // } // } // else{ // state[u] = (state[p] == '1' ? '0' : '1'); // dsu.parent[u] = u; // dsu.size[u] = 1; // if(graph[u].size() == 2){ // for(int v: graph[u]){ // if(v == p)continue; // dfs2(v,u); // } // } // } // } void update(int v, char c){ vi nodes; state[v] = c; for(int i=1; i<=n; i++){ if(dsu.find(i) == dsu.find(v)){ if(state[i] == '?')state[i] = c; nodes.push_back(i); } } for(int el : nodes){ dsu.parent[el] = el; dsu.size[el] = 1; } } void solve() { int q; cin >> n >> q; for(int i=1; i<=n; i++)state[i] = '0'; while(q--){ char cmd; cin >> cmd; switch (cmd){ case '+': { int a,b; cin >> a >> b; if(a == b){ if(state[a] == '?')update(a, '1'); else state[a] = '1'; } else if(state[a] == '?' && state[b] == '1')update(a, '1'); else if(state[a] == '1' && state[b] == '?')update(b, '1'); else if(state[a] == '0' && state[b] == '1')state[a] = '1'; else if(state[a] == '1' && state[b] == '0')state[b] = '1'; else{ state[a] = state[b] = '?'; if(dsu.find(a) != dsu.find(b)){ dsu.merge(a,b); } else{ update(a, '1'); } } } break; case '-': { int c; cin >> c; state[c] = '0'; dsu.size[dsu.find(c)]--; if(dsu.size[dsu.find(c)] == 1){ update(c, '0'); } dsu.parent[c] = c; dsu.size[c] = 1; } break; default: { int d; cin >> d; cout << state[d]; } } } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ; #define debug(x) cout << "[" << #x << " " << x << "] "; #define ar array #define ll long long #define ld long double #define sz(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() typedef vector<int> vi; typedef pair<int,int> pi; const int MAX_N = 3e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; char state[MAX_N]; // vi graph[MAX_N]; struct DSU{ int parent[MAX_N]; int size[MAX_N]; DSU(){ for(int i=0; i<MAX_N; i++){ parent[i] = i; size[i] = 1; } } int find(int x){ if(parent[x] == x)return x; return parent[x] = find(parent[x]); } void merge(int a, int b){ a = find(a); b = find(b); if(a == b)return; if(size[a] < size[b])swap(a,b); parent[b] = a; size[a] += size[b]; } }; int n; DSU dsu = DSU(); // void dfs1(int u, int p = -1){ // for(int v: graph[u]){ // if(state[v] != '?')continue; // // if(state[v] == '?'){ // // state[v] = '1'; // // } // state[v] = '1'; // dsu.parent[v] = v; // dsu.size[v] = 1; // dfs1(v,u); // } // graph[u].clear(); // } // void dfs2(int u, int p = -1){ // if(p == -1){ // if(graph[u].size() == 1){ // dfs2(graph[u][0],u); // } // } // else{ // state[u] = (state[p] == '1' ? '0' : '1'); // dsu.parent[u] = u; // dsu.size[u] = 1; // if(graph[u].size() == 2){ // for(int v: graph[u]){ // if(v == p)continue; // dfs2(v,u); // } // } // } // } void update(int v, char c){ vi nodes; state[v] = c; for(int i=1; i<=n; i++){ if(dsu.find(i) == dsu.find(v)){ if(state[i] == '?')state[i] = c; nodes.push_back(i); } } for(int el : nodes){ dsu.parent[el] = el; dsu.size[el] = 1; } } void solve() { int q; cin >> n >> q; for(int i=1; i<=n; i++)state[i] = '0'; while(q--){ char cmd; cin >> cmd; switch (cmd){ case '+': { int a,b; cin >> a >> b; if(a == b){ if(state[a] == '?')update(a, '1'); else state[a] = '1'; } else if(state[a] == '?' && state[b] == '1')update(a, '1'); else if(state[a] == '1' && state[b] == '?')update(b, '1'); else if(state[a] == '0' && state[b] == '1')state[a] = '1'; else if(state[a] == '1' && state[b] == '0')state[b] = '1'; else{ state[a] = state[b] = '?'; if(dsu.find(a) != dsu.find(b)){ dsu.merge(a,b); } else{ update(a, '1'); } } } break; case '-': { int c; cin >> c; state[c] = '0'; dsu.size[dsu.find(c)]--; if(dsu.size[dsu.find(c)] == 1){ update(c, '0'); } dsu.parent[c] = c; dsu.size[c] = 1; } break; default: { int d; cin >> d; cout << state[d]; } } } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; solve(); } } |