This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #define _GLIBCXX_DEBUG
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define pb push_back
#define f first
#define s second
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = 1e9;
// const ll INF = 1e18;
const ll mod = 1000000007;
// const ll mod = 998244353;
struct dsu{
vi e;
void init(int n){
e = vi(2*n, -1);
}
int get(int x){
return (e[x] < 0 ? x : e[x] = get(e[x]));
}
int sz(int x){
return -e[get(x)];
}
bool same(int x, int y){
return get(x) == get(y);
}
bool unite(int x, int y){
x = get(x), y = get(y);
if(x == y) return 0;
if(e[x] > e[y])
swap(x, y);
e[x] += e[y]; e[y] = x;
return 1;
}
};
void solve(int tc){
int n, q;
cin >> n >> q;
dsu s;
s.init(n);
for(int i = 0; i < q; i++){
char a;
int u, v;
cin >> a >> u >> v;
if(a == 'A'){
s.unite(u-1, v-1+n);
s.unite(u-1+n, v-1);
}
if(a == 'R'){
s.unite(u-1, v-1);
s.unite(u-1+n, v-1+n);
}
if(a == 'Q'){
if(s.same(u-1, v-1) || s.same(u-1+n, v-1+n))
cout << 'R' << '\n';
else if(s.same(u-1+n, v-1) || s.same(u-1, v-1+n))
cout << 'A' << '\n';
else cout << '?' << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
#ifdef LOCAL
auto begin = high_resolution_clock::now();
#endif
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++) solve(t);
#ifdef LOCAL
auto end = high_resolution_clock::now();
cout << fixed << setprecision(4) << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
#endif
return 0;
}
# | 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... |