#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int p[1000005];
int h[1000005];
void reset(int N){
for(int i=0; i<=N; i++){
p[i]=i;
h[i]=1;
}
}
int findSet(int u){
if(u==p[u]){return u;}
else{
p[u]= findSet(p[u]);
return p[u];
}
}
void unionSet(int a, int b){
int A= findSet(a);
int B= findSet(b);
if(A==B){return;}
if(h[A]<h[B]){
p[A]=B;
}else{
p[B]=A;
if(h[A]==h[B]){h[A]++;}
}
}
int p2[1000005];
int h2[1000005];
void reset2(int N){
for(int i=0; i<=N; i++){
p2[i]=i;
h2[i]=1;
}
}
int findSet2(int u){
if(u==p2[u]){return u;}
else{
p2[u]= findSet2(p2[u]);
return p2[u];
}
}
void unionSet2(int a, int b){
int A= findSet2(a);
int B= findSet2(b);
if(A==B){return;}
if(h2[A]<h2[B]){
p2[A]=B;
}else{
p2[B]=A;
if(h2[A]==h2[B]){h2[A]++;}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q; cin >> n >> q;
reset(n); reset2(n);
map<int, int> m;
for(int i=0; i<q; i++){
char a; cin >> a;
int b, c; cin >> b >> c;
if(a=='R'){unionSet(b, c); unionSet2(b, c);}
else if(a=='A'){
unionSet(b, c);
int B= findSet2(b);
int C= findSet2(c);
if(m.count(B)>0){unionSet2(m[B], c);}else{m[B]=C;}
if(m.count(C)>0){unionSet2(m[C], b);}else{m[C]=B;}
}
else{
int bb= findSet(b);
int cc= findSet(c);
if(bb!=cc){cout<< "?" << endl; continue;}
bb= findSet2(b);
cc= findSet2(c);
if(bb==cc){cout<< "R" << endl;}
else{cout<< "A" << endl;}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
5892 KB |
Output is correct |
2 |
Correct |
36 ms |
5992 KB |
Output is correct |
3 |
Correct |
43 ms |
5292 KB |
Output is correct |
4 |
Correct |
37 ms |
5912 KB |
Output is correct |
5 |
Correct |
53 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
4848 KB |
Output is correct |
2 |
Correct |
28 ms |
4780 KB |
Output is correct |
3 |
Correct |
29 ms |
4700 KB |
Output is correct |
4 |
Correct |
42 ms |
4752 KB |
Output is correct |
5 |
Correct |
32 ms |
4768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6148 KB |
Output is correct |
2 |
Correct |
43 ms |
6028 KB |
Output is correct |
3 |
Incorrect |
75 ms |
6892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
388 KB |
Output is correct |
7 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
49 ms |
5892 KB |
Output is correct |
7 |
Correct |
36 ms |
5992 KB |
Output is correct |
8 |
Correct |
43 ms |
5292 KB |
Output is correct |
9 |
Correct |
37 ms |
5912 KB |
Output is correct |
10 |
Correct |
53 ms |
6008 KB |
Output is correct |
11 |
Correct |
25 ms |
4848 KB |
Output is correct |
12 |
Correct |
28 ms |
4780 KB |
Output is correct |
13 |
Correct |
29 ms |
4700 KB |
Output is correct |
14 |
Correct |
42 ms |
4752 KB |
Output is correct |
15 |
Correct |
32 ms |
4768 KB |
Output is correct |
16 |
Correct |
31 ms |
6148 KB |
Output is correct |
17 |
Correct |
43 ms |
6028 KB |
Output is correct |
18 |
Incorrect |
75 ms |
6892 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |