This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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)!!!
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |