Submission #680783

#TimeUsernameProblemLanguageResultExecution timeMemory
680783BhavayGoyalExperimental Charges (NOI19_charges)C++14
100 / 100
23 ms1560 KiB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 1e5+5;
pii par[N];
int sz[N];

pii find(int v) {
    if (par[v].f == v) return par[v];
    pii pr = find(par[v].f);
    return par[v] = {pr.f, pr.s^par[v].s};
}

void Union(int a, int b, bool f) {
    pii aa = find(a); pii bb = find(b);
    if (aa.f == bb.f) return;
    if (sz[aa.f] < sz[bb.f]) swap(aa, bb);
    par[bb.f] = {aa.f, aa.s^bb.s^f};
    sz[aa.f] += sz[bb.f];
}

void sol() {
    int n, q; cin >> n >> q;
    for (int i = 0; i <= n; i++) par[i] = {i, 0}, sz[i] = 1;
    while (q--) {
        char x; cin >> x;
        int a, b; cin >> a >> b;
        if (x == 'Q') {
            pii aa = find(a); pii bb = find(b);
            if (aa.f != bb.f) cout << "?" << endl;
            else cout << (aa.s!=bb.s?"A":"R") << endl;
        }
        else Union(a, b, (x=='A'));
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...