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;
vector<bool> added;
DSU(int sz) {
size.resize(sz+1, 1);
link.resize(sz+1);
added.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) {
added[a] = true, added[b] = true;
a = find(a);
b = find(b);
if (a == b) return;
if (size[a] < size[b]) swap(a, b);
size[a] += size[b];
link[b] = a;
}
};
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);
else if (c == 'A') d.added[a] = true, d.added[b] = true;
else if (c == 'Q') {
if (d.same(a, b)) cout << "R\n";
else if (d.added[a] && d.added[b]) 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... |