This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[a] < size[b]) swap(a, b);
if ((charge[a] == charge[b] && res) || (charge[a] != charge[b] && !res)) {
a = find(a);
b = find(b);
dfs(b, b);
}
else {
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);
// freopen("in.txt", "r", stdin);
int n, q;
cin >> n >> q;
DSU d(n);
while (q--) {
char c;
int a, b;
cin >> c >> a >> b;
if (c == 'R') {
if (d.same(a, b)) continue;
else d.unite(a, b, 0);
}
else if (c == 'A') {
if (d.same(a, b)) continue;
else 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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |