This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()
#define lc(i) 2*i
#define rc(i) 2*i+1
//#define int long long
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;
struct DSU {
int n;
vii par;
vi s;
void init(int x) {
n = x;
par.resize(n);
for (int i = 0; i < n; i++) par[i] = ii(i, 0);
s.assign(n, 1);
}
ii get(int x) {
if (par[x].first == x) return ii(x, 0);
ii fp = get(par[x].first);
return par[x] = ii(fp.first, fp.second ^ par[x].second);
}
bool unite(int x, int y, int charge) {
ii a = get(x);
ii b = get(y);
if (a.first == b.first) return false;
if (s[a.first] < s[b.first]) swap(x, y);
s[a.first] += s[b.first];
par[b.first] = ii(a.first, charge ^ a.second ^ b.second);
return true;
}
int getSz(int x) {
ii a = get(x);
return s[a.first];
}
};
signed main() {
FAST_IO;
#ifdef arujbansal_local
setIO("input.txt", "output.txt");
#endif
int n, q;
cin >> n >> q;
DSU dsu;
dsu.init(n);
while (q--) {
char type;
int u, v;
cin >> type >> u >> v;
u--, v--;
if (type == 'Q') {
ii a = dsu.get(u);
ii b = dsu.get(v);
if (a.first == b.first)
cout << (a.second == b.second ? 'R' : 'A') << "\n";
else cout << '?' << "\n";
} else dsu.unite(u, v, type == 'A');
}
}
# | 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... |