#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1268 KB |
Output is correct |
2 |
Correct |
11 ms |
1104 KB |
Output is correct |
3 |
Correct |
12 ms |
1276 KB |
Output is correct |
4 |
Correct |
12 ms |
1104 KB |
Output is correct |
5 |
Correct |
13 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1360 KB |
Output is correct |
2 |
Correct |
13 ms |
1616 KB |
Output is correct |
3 |
Incorrect |
16 ms |
2536 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
1104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
11 ms |
1268 KB |
Output is correct |
7 |
Correct |
11 ms |
1104 KB |
Output is correct |
8 |
Correct |
12 ms |
1276 KB |
Output is correct |
9 |
Correct |
12 ms |
1104 KB |
Output is correct |
10 |
Correct |
13 ms |
1104 KB |
Output is correct |
11 |
Correct |
13 ms |
1360 KB |
Output is correct |
12 |
Correct |
13 ms |
1616 KB |
Output is correct |
13 |
Incorrect |
16 ms |
2536 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |