#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define endl "\n"
void printVector(vector<int> a){
for (auto x: a) cout << x << " ";
cout << endl;
}
struct UFDS{
vector<int> link, siz, charge, mark;
UFDS (int n){
link.resize(n+1); siz.resize(n+1, 1); charge.resize(n+1); mark.resize(n+1);
for (int i = 0; i <= n; ++i) link[i] = i;
}
int findRoot(int x){
if (x == link[x]) return x;
return findRoot(link[x]);
}
int OfindCharge(int x){
if (x == link[x]) return mark[x];
return mark[x]^OfindCharge(link[x]);
}
int findCharge(int x){
return OfindCharge(x);
}
void unitea(int x, int y){
int xr = findRoot(x), yr = findRoot(y);
if (xr == yr) return;
if (findCharge(x) == findCharge(y)){
if (siz[xr] < siz[yr]) swap(xr, yr);
link[yr] = xr;
siz[xr] += siz[yr];
mark[yr] = 1;
}else{
if (siz[xr] < siz[yr]) swap(xr, yr);
link[yr] = xr;
siz[xr] += siz[yr];
}
}
void uniter(int x, int y){
int xr = findRoot(x), yr = findRoot(y);
if (xr == yr) return;
if (findCharge(x) == findCharge(y)){
if (siz[xr] < siz[yr]) swap(xr, yr);
link[yr] = xr;
siz[xr] += siz[yr];
}else{
if (siz[xr] < siz[yr]) swap(xr, yr);
link[yr] = xr;
siz[xr] += siz[yr];
mark[yr] = 1;
}
}
};
void solve(){
int n, q; cin >> n >> q;
UFDS ds(n+1);
// cout << "a" << endl;
while (q--){
char t; int a, b; cin >> t >> a >> b;
if (t == 'A'){
ds.unitea(a, b);
}else if (t == 'R'){
ds.uniter(a, b);
}else{
// cout << "i";
// int a, b; cin >> a >> b;
if (ds.findRoot(a) != ds.findRoot(b)){
cout << "?" << endl;
continue;
}
if (ds.findCharge(a) == ds.findCharge(b)){
cout << "R" << endl;
}else{
cout << "A" << endl;
}
}
}
// FOR(i,1,n+1) cout << ds.findCharge(i) << " ";
cout << endl;
}
int32_t main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int t = 1; // cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4176 KB |
Output is correct |
2 |
Correct |
16 ms |
4176 KB |
Output is correct |
3 |
Correct |
18 ms |
4160 KB |
Output is correct |
4 |
Correct |
16 ms |
4432 KB |
Output is correct |
5 |
Correct |
16 ms |
4312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
4944 KB |
Output is correct |
2 |
Correct |
17 ms |
4688 KB |
Output is correct |
3 |
Correct |
20 ms |
4688 KB |
Output is correct |
4 |
Correct |
20 ms |
4824 KB |
Output is correct |
5 |
Correct |
20 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
4688 KB |
Output is correct |
2 |
Correct |
20 ms |
4688 KB |
Output is correct |
3 |
Correct |
19 ms |
4432 KB |
Output is correct |
4 |
Correct |
20 ms |
4908 KB |
Output is correct |
5 |
Correct |
21 ms |
4736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
15 ms |
4176 KB |
Output is correct |
7 |
Correct |
16 ms |
4176 KB |
Output is correct |
8 |
Correct |
18 ms |
4160 KB |
Output is correct |
9 |
Correct |
16 ms |
4432 KB |
Output is correct |
10 |
Correct |
16 ms |
4312 KB |
Output is correct |
11 |
Correct |
18 ms |
4944 KB |
Output is correct |
12 |
Correct |
17 ms |
4688 KB |
Output is correct |
13 |
Correct |
20 ms |
4688 KB |
Output is correct |
14 |
Correct |
20 ms |
4824 KB |
Output is correct |
15 |
Correct |
20 ms |
4944 KB |
Output is correct |
16 |
Correct |
19 ms |
4688 KB |
Output is correct |
17 |
Correct |
20 ms |
4688 KB |
Output is correct |
18 |
Correct |
19 ms |
4432 KB |
Output is correct |
19 |
Correct |
20 ms |
4908 KB |
Output is correct |
20 |
Correct |
21 ms |
4736 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
17 ms |
4688 KB |
Output is correct |
27 |
Correct |
18 ms |
4860 KB |
Output is correct |
28 |
Correct |
18 ms |
4432 KB |
Output is correct |
29 |
Correct |
25 ms |
4688 KB |
Output is correct |
30 |
Correct |
18 ms |
4688 KB |
Output is correct |