#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i)
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define nl "\n"
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
vl lnk, sz, opp;
ll find(ll a) {
return (a == lnk[a] ? a : lnk[a] = find(lnk[a]));
}
ll unite(ll a, ll b) {
if(a == -1)
return b;
else if(b == -1)
return a;
a = find(a);
b = find(b);
if(a == b)
return a;
if(sz[a] < sz[b])
swap(a, b);
lnk[b] = a;
sz[a] += sz[b];
return a;
}
void solve()
{
ll n, m;
cin >> n >> m;
// cout << n << ' ' << m << nl;
lnk.resize(n + 1);
sz.resize(n + 1);
opp.resize(n + 1);
fur(i, 0, n) {
lnk[i] = i;
sz[i] = 1;
opp[i] = -1;
}
while(m--) {
char c;
ll a, b;
cin >> c >> a >> b;
if(c == 'A') {
a = find(a);
b = find(b);
if(a == b)
continue;
ll c = opp[a]; // a attracts c
ll d = opp[b]; // b attracts d
if(c != -1)
b = unite(b, c); // a attracts b and c
if(d != -1)
a = unite(a, d); // b attracts a and d
opp[a] = b;
opp[b] = a;
}
else if(c == 'R') {
a = find(a);
b = find(b);
if(opp[a] == b)
continue;
ll c = opp[a];
ll d = opp[b];
c = unite(c, d);
a = unite(a, b);
opp[a] = c;
if(c != -1)
opp[c] = a;
}
else {
a = find(a);
b = find(b);
if(a == b)
cout << 'R' << nl;
else if(opp[a] == b || opp[b] == a)
cout << 'A' << nl;
else
cout << '?' << nl;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// cin >> t;
while(t--)
{
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2592 KB |
Output is correct |
2 |
Correct |
19 ms |
2556 KB |
Output is correct |
3 |
Correct |
27 ms |
2592 KB |
Output is correct |
4 |
Correct |
27 ms |
2516 KB |
Output is correct |
5 |
Correct |
22 ms |
2588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2684 KB |
Output is correct |
2 |
Correct |
21 ms |
2644 KB |
Output is correct |
3 |
Correct |
22 ms |
2644 KB |
Output is correct |
4 |
Correct |
27 ms |
2644 KB |
Output is correct |
5 |
Correct |
35 ms |
2724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2516 KB |
Output is correct |
2 |
Correct |
28 ms |
3756 KB |
Output is correct |
3 |
Correct |
27 ms |
3668 KB |
Output is correct |
4 |
Correct |
28 ms |
4024 KB |
Output is correct |
5 |
Correct |
24 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
21 ms |
2592 KB |
Output is correct |
7 |
Correct |
19 ms |
2556 KB |
Output is correct |
8 |
Correct |
27 ms |
2592 KB |
Output is correct |
9 |
Correct |
27 ms |
2516 KB |
Output is correct |
10 |
Correct |
22 ms |
2588 KB |
Output is correct |
11 |
Correct |
21 ms |
2684 KB |
Output is correct |
12 |
Correct |
21 ms |
2644 KB |
Output is correct |
13 |
Correct |
22 ms |
2644 KB |
Output is correct |
14 |
Correct |
27 ms |
2644 KB |
Output is correct |
15 |
Correct |
35 ms |
2724 KB |
Output is correct |
16 |
Correct |
21 ms |
2516 KB |
Output is correct |
17 |
Correct |
28 ms |
3756 KB |
Output is correct |
18 |
Correct |
27 ms |
3668 KB |
Output is correct |
19 |
Correct |
28 ms |
4024 KB |
Output is correct |
20 |
Correct |
24 ms |
3788 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
22 ms |
3944 KB |
Output is correct |
27 |
Correct |
23 ms |
3856 KB |
Output is correct |
28 |
Correct |
23 ms |
3792 KB |
Output is correct |
29 |
Correct |
22 ms |
3724 KB |
Output is correct |
30 |
Correct |
24 ms |
3796 KB |
Output is correct |