#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<long long>
#define all(x) x.begin(), x.end()
#define endl "\n"
void solution();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
const int N = 1e5+10;
ll rep[N], sz[N];
bool charge[N];
set<int> child[N];
int find(int a) {
while (a != rep[a]) a = rep[a];
return a;
}
bool isSame(int a, int b) {
return find(a) == find(b);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
rep[b] = a;
sz[a] += sz[b];
child[a].insert(b);
}
void flip(int a, set<int> &vis) {
charge[a] ^= 1;
vis.insert(a);
for (int b : child[a]) {
if (vis.count(b)) continue;
flip(b, vis);
}
}
void solution() {
iota(rep, rep+N, 0);
fill(sz, sz+N, 1);
ll n, q; cin >> n >> q;
while (q--) {
char t; cin >> t;
int a, b; cin >> a >> b;
if (t == 'R') {
if (charge[a] == charge[b]) {
unite(a, b);
} else {
a = find(a);
b = find(b);
if (sz[a] < sz[b]) swap(a, b);
set<int> vis;
flip(b, vis);
unite(a, b);
}
} else if (t == 'A') {
if (charge[a] != charge[b]) unite(a, b);
else {
a = find(a);
b = find(b);
if (sz[a] < sz[b]) swap(a, b);
set<int> vis;
flip(b, vis);
unite(a, b);
}
} else {
if (!isSame(a, b)) {
cout << "?" << endl;
} else {
cout << (charge[a] == charge[b] ? "R" : "A") << endl;
}
}
}
}
| # | 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... |