Submission #1220983

#TimeUsernameProblemLanguageResultExecution timeMemory
1220983toast12Experimental Charges (NOI19_charges)C++20
100 / 100
28 ms6216 KiB
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> size, link, charge; vector<vector<int>> adj; DSU(int sz) { size.resize(sz+1, 1); link.resize(sz+1); charge.resize(sz+1, 1); adj.resize(sz+1); iota(link.begin(), link.end(), 0); } int find(int x) { while (x != link[x]) x = link[x]; return x; } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b, bool res) { if (same(a, b)) return; if (size[find(a)] < size[find(b)]) swap(a, b); if ((charge[a] == charge[b] && res) || (charge[a] != charge[b] && !res)) { b = find(b); dfs(b, b); } a = find(a); b = find(b); size[a] += size[b]; link[b] = a; adj[a].push_back(b); adj[b].push_back(a); } void dfs(int cur, int p) { charge[cur] = 1-charge[cur]; for (auto e : adj[cur]) { if (e != p) dfs(e, cur); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, q; cin >> n >> q; DSU d(n); while (q--) { char c; int a, b; cin >> c >> a >> b; if (c == 'R') d.unite(a, b, 0); else if (c == 'A') d.unite(a, b, 1); else { if (d.same(a, b)) { if (d.charge[a] == d.charge[b]) cout << "R\n"; else cout << "A\n"; } else cout << "?\n"; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...