// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define read_vector(v) for(auto &x : v){cin >> x;}
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int ll
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
//
//
const int INF = 1e18;
const int N = 2000005;
vector<int> p, ranks, opp;
void build() {
p.assign(N+2, 0);
ranks.assign(N+2, 0);
opp.assign(N+2, 0);
for(int i=1; i<=N; ++i)
p[i] = i;
}
int find_set(int i) {
if(p[i] == i)
return i;
return p[i] = find_set(p[i]);
}
bool same(int i, int j) {
return find_set(i) == find_set(j);
}
void unite(int i, int j) {
if(same(i,j)) return;
int x = find_set(i), y = find_set(j);
int k = opp[x];
if(ranks[x] > y) // ensure x < y
swap(x,y);
p[x] = y;
if(ranks[x] == ranks[y])
++ranks[y];
opp[y] = k;
}
void solve() {
int n, q;
cin >> n >> q;
vector<int> opp(n+2);
build();
while(q--) {
char t; int a, b;
cin >> t >> a >> b;
int oppa = opp[find_set(a)], oppb = opp[find_set(b)];
if(t == 'A') {
unite(a + n, b + n);
if(oppa)
unite(oppa, b);
if(oppb)
unite(oppb, a);
opp[find_set(a)] = b;
opp[find_set(b)] = a;
} else if(t == 'R') {
unite(a + n, b + n);
if(oppa && oppb)
unite(oppa, oppb);
else {
if(oppa) oppb = oppa;
if(oppb) oppa = oppb;
}
unite(a, b);
opp[find_set(a)] = oppa;
opp[find_set(oppa)] = a;
} else {
if(same(a + n, b + n)) {
if(same(a, b))
cout << "R" << '\n';
else
cout << "A" << '\n';
} else {
cout << "?" << '\n';
}
}
}
}
signed main() {
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
while(T--)
solve();
return 0;
}
/* CHECK :
1. COMPARATOR FUNCTION MUST RETURN FALSE WHEN ARGUMENTS ARE EQUAL!!!
2. Overflow! Typecase int64_t on operations if varaibles are int
3. Check array bounds!!!
4. Check array indexing!!!
5. Edge cases. (N==1)!!!
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
47184 KB |
Output is correct |
2 |
Correct |
10 ms |
47392 KB |
Output is correct |
3 |
Correct |
10 ms |
47184 KB |
Output is correct |
4 |
Correct |
9 ms |
47324 KB |
Output is correct |
5 |
Correct |
11 ms |
47184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
48960 KB |
Output is correct |
2 |
Correct |
33 ms |
48888 KB |
Output is correct |
3 |
Correct |
32 ms |
48976 KB |
Output is correct |
4 |
Correct |
31 ms |
48988 KB |
Output is correct |
5 |
Correct |
35 ms |
48956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
49480 KB |
Output is correct |
2 |
Correct |
36 ms |
49480 KB |
Output is correct |
3 |
Correct |
34 ms |
49388 KB |
Output is correct |
4 |
Correct |
36 ms |
49460 KB |
Output is correct |
5 |
Correct |
38 ms |
49528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
49736 KB |
Output is correct |
2 |
Correct |
33 ms |
49488 KB |
Output is correct |
3 |
Correct |
39 ms |
49412 KB |
Output is correct |
4 |
Correct |
40 ms |
49500 KB |
Output is correct |
5 |
Correct |
38 ms |
49488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
47184 KB |
Output is correct |
2 |
Correct |
10 ms |
47392 KB |
Output is correct |
3 |
Correct |
10 ms |
47184 KB |
Output is correct |
4 |
Correct |
9 ms |
47324 KB |
Output is correct |
5 |
Correct |
11 ms |
47184 KB |
Output is correct |
6 |
Correct |
12 ms |
47388 KB |
Output is correct |
7 |
Correct |
11 ms |
47440 KB |
Output is correct |
8 |
Correct |
17 ms |
47376 KB |
Output is correct |
9 |
Correct |
18 ms |
47440 KB |
Output is correct |
10 |
Correct |
17 ms |
47412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
47184 KB |
Output is correct |
2 |
Correct |
10 ms |
47392 KB |
Output is correct |
3 |
Correct |
10 ms |
47184 KB |
Output is correct |
4 |
Correct |
9 ms |
47324 KB |
Output is correct |
5 |
Correct |
11 ms |
47184 KB |
Output is correct |
6 |
Correct |
30 ms |
48960 KB |
Output is correct |
7 |
Correct |
33 ms |
48888 KB |
Output is correct |
8 |
Correct |
32 ms |
48976 KB |
Output is correct |
9 |
Correct |
31 ms |
48988 KB |
Output is correct |
10 |
Correct |
35 ms |
48956 KB |
Output is correct |
11 |
Correct |
33 ms |
49480 KB |
Output is correct |
12 |
Correct |
36 ms |
49480 KB |
Output is correct |
13 |
Correct |
34 ms |
49388 KB |
Output is correct |
14 |
Correct |
36 ms |
49460 KB |
Output is correct |
15 |
Correct |
38 ms |
49528 KB |
Output is correct |
16 |
Correct |
29 ms |
49736 KB |
Output is correct |
17 |
Correct |
33 ms |
49488 KB |
Output is correct |
18 |
Correct |
39 ms |
49412 KB |
Output is correct |
19 |
Correct |
40 ms |
49500 KB |
Output is correct |
20 |
Correct |
38 ms |
49488 KB |
Output is correct |
21 |
Correct |
12 ms |
47388 KB |
Output is correct |
22 |
Correct |
11 ms |
47440 KB |
Output is correct |
23 |
Correct |
17 ms |
47376 KB |
Output is correct |
24 |
Correct |
18 ms |
47440 KB |
Output is correct |
25 |
Correct |
17 ms |
47412 KB |
Output is correct |
26 |
Correct |
34 ms |
49396 KB |
Output is correct |
27 |
Correct |
42 ms |
49488 KB |
Output is correct |
28 |
Correct |
42 ms |
49224 KB |
Output is correct |
29 |
Correct |
41 ms |
49224 KB |
Output is correct |
30 |
Correct |
45 ms |
49224 KB |
Output is correct |