Submission #680781

# Submission time Handle Problem Language Result Execution time Memory
680781 2023-01-11T19:04:22 Z BhavayGoyal Experimental Charges (NOI19_charges) C++14
100 / 100
27 ms 2872 KB
#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 {v, 0};
    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 time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2456 KB Output is correct
2 Correct 17 ms 2320 KB Output is correct
3 Correct 19 ms 2388 KB Output is correct
4 Correct 23 ms 2344 KB Output is correct
5 Correct 17 ms 2364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2868 KB Output is correct
2 Correct 20 ms 2768 KB Output is correct
3 Correct 23 ms 2872 KB Output is correct
4 Correct 24 ms 2768 KB Output is correct
5 Correct 25 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 2764 KB Output is correct
2 Correct 21 ms 2704 KB Output is correct
3 Correct 21 ms 2664 KB Output is correct
4 Correct 27 ms 2840 KB Output is correct
5 Correct 25 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 22 ms 2456 KB Output is correct
7 Correct 17 ms 2320 KB Output is correct
8 Correct 19 ms 2388 KB Output is correct
9 Correct 23 ms 2344 KB Output is correct
10 Correct 17 ms 2364 KB Output is correct
11 Correct 23 ms 2868 KB Output is correct
12 Correct 20 ms 2768 KB Output is correct
13 Correct 23 ms 2872 KB Output is correct
14 Correct 24 ms 2768 KB Output is correct
15 Correct 25 ms 2808 KB Output is correct
16 Correct 20 ms 2764 KB Output is correct
17 Correct 21 ms 2704 KB Output is correct
18 Correct 21 ms 2664 KB Output is correct
19 Correct 27 ms 2840 KB Output is correct
20 Correct 25 ms 2764 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 24 ms 2776 KB Output is correct
27 Correct 22 ms 2828 KB Output is correct
28 Correct 20 ms 2708 KB Output is correct
29 Correct 25 ms 2704 KB Output is correct
30 Correct 21 ms 2700 KB Output is correct