#include <bits/stdc++.h>
class UFDS {
public:
std::vector<int> par;
UFDS(int n) : par(n + 1) { std::iota(par.begin(), par.end(), 0); }
int root(int x) { return x == par[x] ? x : par[x] = root(par[x]); }
void merge(int x, int y) {
x = root(x), y = root(y);
if (x == y) {
return;
}
par[y] = x;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
UFDS dsu(2 * n);
while (q--) {
char c;
std::cin >> c;
int a, b;
std::cin >> a >> b;
if (c == 'A') {
dsu.merge(a, b + n);
dsu.merge(a + n, b);
} else if (c == 'R') {
dsu.merge(a, b);
dsu.merge(a + n, b + n);
} else {
if (dsu.root(a) == dsu.root(b)) {
std::cout << "R\n";
} else if (dsu.root(a) == dsu.root(b + n)) {
std::cout << "A\n";
} else {
std::cout << "?\n";
}
}
}
}
# | 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... |