#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
#define FOR(i, j, k) for (ll i = j; i < (ll) k; ++i)
#define FORD(i, j, k) for (ll i = j; i >= (ll) k; --i)
#define nl "\n"
#define all(x) (x).begin(), (x).end()
#define sc second
#define fr first
#define pb push_back
ll link[100005] = {};
ll sz[100005] = {};
ll opp[100005] = {};
ll find(ll x) {
return (link[x] == x ? x : link[x] = find(link[x]));
}
ll unite(ll a, ll b) {
a = find(a);
b = find(b);
if(a == b)
return a;
if(sz[a] < sz[b])
swap(a, b);
sz[a] += sz[b];
link[b] = a;
return a;
}
void setA(ll a, ll b){
a = find(a);
b = find(b);
if(a == b)
return;
if(opp[a] == -1)
opp[a] = b;
else
opp[a] = unite(opp[a], b);
if(opp[b] == -1)
opp[b] = a;
else
opp[b] = unite(opp[b], a);
}
bool areA(ll a, ll b) {
a = find(a);
b = find(b);
if(opp[a] != -1 && find(opp[a]) == b)
return true;
return false;
}
void setR(ll a, ll b) {
a = find(a);
b = find(b);
if(areA(a, b))
return;
if(a == b)
return;
ll o;
if(opp[a] != -1 && opp[b] != -1)
o = unite(opp[a], opp[b]);
else if(opp[a] != -1)
o = find(opp[a]);
else if(opp[b] != -1)
o = find(opp[b]);
else
o = -1;
ll fin = unite(a, b);
opp[fin] = o;
}
char check(ll a, ll b) {
if(find(a) == find(b))
return 'R';
if(areA(a, b))
return 'A';
return '?';
}
void solve()
{
int n, q;
cin >> n >> q;
FOR(i, 1, n + 1) {
link[i] = i;
sz[i] = 1;
opp[i] = -1;
}
while(q--) {
char t;
int a, b;
cin >> t >> a >> b;
if(t == 'A')
setA(a, b);
else if(t == 'R')
setR(a, b);
else
cout << check(a, b) << nl;
}
}
int main()
{
// clock_t start, end;
// start = clock();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
// cin >> t;
while (t--)
{
solve();
}
// end = clock();
// cout << nl << "Time : " << double(end - start)/double(CLOCKS_PER_SEC) << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3464 KB |
Output is correct |
2 |
Correct |
18 ms |
3412 KB |
Output is correct |
3 |
Correct |
22 ms |
3412 KB |
Output is correct |
4 |
Correct |
19 ms |
3472 KB |
Output is correct |
5 |
Correct |
18 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4064 KB |
Output is correct |
2 |
Correct |
21 ms |
3916 KB |
Output is correct |
3 |
Correct |
22 ms |
3940 KB |
Output is correct |
4 |
Correct |
22 ms |
3980 KB |
Output is correct |
5 |
Correct |
24 ms |
4044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
3980 KB |
Output is correct |
2 |
Correct |
22 ms |
3804 KB |
Output is correct |
3 |
Correct |
22 ms |
3788 KB |
Output is correct |
4 |
Correct |
23 ms |
4020 KB |
Output is correct |
5 |
Correct |
29 ms |
3980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
18 ms |
3464 KB |
Output is correct |
7 |
Correct |
18 ms |
3412 KB |
Output is correct |
8 |
Correct |
22 ms |
3412 KB |
Output is correct |
9 |
Correct |
19 ms |
3472 KB |
Output is correct |
10 |
Correct |
18 ms |
3540 KB |
Output is correct |
11 |
Correct |
22 ms |
4064 KB |
Output is correct |
12 |
Correct |
21 ms |
3916 KB |
Output is correct |
13 |
Correct |
22 ms |
3940 KB |
Output is correct |
14 |
Correct |
22 ms |
3980 KB |
Output is correct |
15 |
Correct |
24 ms |
4044 KB |
Output is correct |
16 |
Correct |
22 ms |
3980 KB |
Output is correct |
17 |
Correct |
22 ms |
3804 KB |
Output is correct |
18 |
Correct |
22 ms |
3788 KB |
Output is correct |
19 |
Correct |
23 ms |
4020 KB |
Output is correct |
20 |
Correct |
29 ms |
3980 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 |
340 KB |
Output is correct |
26 |
Correct |
22 ms |
3916 KB |
Output is correct |
27 |
Correct |
24 ms |
3972 KB |
Output is correct |
28 |
Correct |
22 ms |
3784 KB |
Output is correct |
29 |
Correct |
22 ms |
3796 KB |
Output is correct |
30 |
Correct |
24 ms |
3720 KB |
Output is correct |