#include <bits/stdc++.h>
using namespace std;
// short forms //
#define nl "\n"
#define pb push_back
#define f first
#define s second
#define ll long long int
#define ld long double
#define mp make_pair
#define all(x) x.begin(), x.end()
// debug //
#define pv(x) for(auto k : x){cout << k << " ";} cout << nl;
#define pp(x) cout << x.f << " " << x.s << nl;
#define bv(x) if (x) cout << "true"; else cout << "false";
#define gp(x, y) x << " " << y
// loop //
#define FOR(i, a, b) for(ll i = a; i <=b; i++)
// pair //
typedef pair<ll, ll> pl;
// vector //
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pl> vp;
// constants //
const ll INF = 1e17;
const ll mod = 1e9 + 7;
const ll S = 1e5 + 5;
ll n, siz[S], link[S], eof[S];
ll find(ll a){
while(a != link[a]) a= link[a];
return a;
}
ll unite(ll a, ll b){ // returns the rep of the bigger set
if (a == -1)
return b;
else if (b == -1)
return a;
a = find(a);
b = find(b);
if (a == b) return a;
// union by size
if (siz[a] < siz[b]) swap(a, b);
siz[a] += siz[b];
link[b] = a;
return a;
}
void solve(){
FOR(i, 0, S-1){
link[i] = i;
siz[i] = 1;
eof[i] = -1;
}
ll q;
cin >> n >> q;
while(q--){
char t;
ll a, b;
cin >> t;
cin >> a >> b;
if (t == 'A'){
// set enemies
a = find(a);
b = find(b);
if (a == b)
continue;
ll c = eof[a];
ll d = eof[b];
if (c != -1){
b = unite(b, c);
}
if (d != -1)
a = unite(a, d);
eof[a] = b;
eof[b] = a;
}
else if (t == 'R'){
// set friends
a = find(a);
b = find(b);
if (eof[a] == b)
continue;
if (siz[a] < siz[b]) swap(a, b);
unite(a, b);
// unite the friends
// unite the enemies
if (eof[a] != -1 && eof[b] != -1)
unite(eof[a], eof[b]);
if (eof[a] == -1)
eof[a] = eof[b];
}
else{
// query
a = find(a);
b = find(b);
if (a == b)
cout << 'R' << nl;
else if (eof[a] == b || eof[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();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
2644 KB |
Output is correct |
2 |
Correct |
19 ms |
2644 KB |
Output is correct |
3 |
Correct |
23 ms |
2724 KB |
Output is correct |
4 |
Correct |
27 ms |
2740 KB |
Output is correct |
5 |
Correct |
20 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2772 KB |
Output is correct |
2 |
Correct |
20 ms |
2680 KB |
Output is correct |
3 |
Correct |
26 ms |
2760 KB |
Output is correct |
4 |
Correct |
25 ms |
2696 KB |
Output is correct |
5 |
Correct |
24 ms |
2716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2736 KB |
Output is correct |
2 |
Correct |
22 ms |
2724 KB |
Output is correct |
3 |
Correct |
21 ms |
2644 KB |
Output is correct |
4 |
Correct |
36 ms |
2664 KB |
Output is correct |
5 |
Correct |
25 ms |
2672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
28 ms |
2644 KB |
Output is correct |
7 |
Correct |
19 ms |
2644 KB |
Output is correct |
8 |
Correct |
23 ms |
2724 KB |
Output is correct |
9 |
Correct |
27 ms |
2740 KB |
Output is correct |
10 |
Correct |
20 ms |
2644 KB |
Output is correct |
11 |
Correct |
19 ms |
2772 KB |
Output is correct |
12 |
Correct |
20 ms |
2680 KB |
Output is correct |
13 |
Correct |
26 ms |
2760 KB |
Output is correct |
14 |
Correct |
25 ms |
2696 KB |
Output is correct |
15 |
Correct |
24 ms |
2716 KB |
Output is correct |
16 |
Correct |
24 ms |
2736 KB |
Output is correct |
17 |
Correct |
22 ms |
2724 KB |
Output is correct |
18 |
Correct |
21 ms |
2644 KB |
Output is correct |
19 |
Correct |
36 ms |
2664 KB |
Output is correct |
20 |
Correct |
25 ms |
2672 KB |
Output is correct |
21 |
Correct |
3 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |