#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
typedef pair<int,int> pi;
#ifdef LOCAL
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 69
#endif
template <typename Arg>
void __f(string name, Arg arg) {
cerr << name << " = " << arg << endl;
}
template <typename Head, typename... Tail>
void __f(string names, Head head, Tail... tail) {
string cur = "";
for (auto ch: names){if(ch==','){break;}else{cur+=ch;}}
string nxt = names.substr(cur.size()+2);
cerr << cur << " = " << head << ", ";
__f(nxt, tail...);
}
const int mxn = 1e5 + 5;
int n, q, sz[mxn];
pi par[mxn];
pi root(int x) {
if (par[x].first == x) return pi(x, 0);
pi ct = root(par[x].first);
return par[x] = pi(ct.first, ct.second ^ par[x].second);
}
bool same(int x, int y) {return root(x) == root(y);}
void unite(int x, int y, int charge) {
pi xx = root(x), yy = root(y);
int a = xx.first, b = yy.first;
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b].first = a;
par[b].second = xx.second ^ yy.second ^ charge;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) par[i] = pi(i, 1), sz[i] = 1;
while (q--) {
char op; int a, b;
cin >> op >> a >> b;
if (op == 'Q') {
pi aa = root(a);
pi bb = root(b);
if (aa.first != bb.first) cout << "?\n";
else if (aa.second == bb.second) cout << "R\n";
else cout << "A\n";
} else {
if (op == 'A') unite(a, b, 1);
else unite(a, b, 0);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3436 KB |
Output is correct |
2 |
Correct |
24 ms |
3564 KB |
Output is correct |
3 |
Correct |
29 ms |
3564 KB |
Output is correct |
4 |
Correct |
36 ms |
3564 KB |
Output is correct |
5 |
Correct |
30 ms |
3692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
3180 KB |
Output is correct |
2 |
Correct |
28 ms |
3180 KB |
Output is correct |
3 |
Correct |
31 ms |
3052 KB |
Output is correct |
4 |
Correct |
31 ms |
3052 KB |
Output is correct |
5 |
Correct |
31 ms |
3180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3564 KB |
Output is correct |
2 |
Correct |
28 ms |
3948 KB |
Output is correct |
3 |
Correct |
35 ms |
3820 KB |
Output is correct |
4 |
Correct |
29 ms |
4076 KB |
Output is correct |
5 |
Correct |
30 ms |
3948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
27 ms |
3436 KB |
Output is correct |
7 |
Correct |
24 ms |
3564 KB |
Output is correct |
8 |
Correct |
29 ms |
3564 KB |
Output is correct |
9 |
Correct |
36 ms |
3564 KB |
Output is correct |
10 |
Correct |
30 ms |
3692 KB |
Output is correct |
11 |
Correct |
26 ms |
3180 KB |
Output is correct |
12 |
Correct |
28 ms |
3180 KB |
Output is correct |
13 |
Correct |
31 ms |
3052 KB |
Output is correct |
14 |
Correct |
31 ms |
3052 KB |
Output is correct |
15 |
Correct |
31 ms |
3180 KB |
Output is correct |
16 |
Correct |
27 ms |
3564 KB |
Output is correct |
17 |
Correct |
28 ms |
3948 KB |
Output is correct |
18 |
Correct |
35 ms |
3820 KB |
Output is correct |
19 |
Correct |
29 ms |
4076 KB |
Output is correct |
20 |
Correct |
30 ms |
3948 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
26 ms |
4076 KB |
Output is correct |
27 |
Correct |
28 ms |
4076 KB |
Output is correct |
28 |
Correct |
27 ms |
3820 KB |
Output is correct |
29 |
Correct |
27 ms |
3820 KB |
Output is correct |
30 |
Correct |
29 ms |
3820 KB |
Output is correct |