Submission #374485

# Submission time Handle Problem Language Result Execution time Memory
374485 2021-03-07T10:31:30 Z vishesh312 Experimental Charges (NOI19_charges) C++17
100 / 100
38 ms 3308 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()

using ll = long long;
const int mod = 1e9+7;

struct UFDS {
    vector<int> siz, link;
    UFDS(int n) {
        siz.resize(n, 1);
        link.resize(n);
        for (int i = 0; i < n; ++i) link[i] = i;
    }
    int find(int x) {
        while (x != link[x]) x = link[x];
        return x;
    }
    bool same(int a, int b) {
        return find(a) == find(b);
    }
    void unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) return;
        if (siz[a] < siz[b]) swap(a, b);
        siz[a] += siz[b];
        link[b] = a;
    }
};

void solve(int tc) {
    int n, q;
    cin >> n >> q;
    UFDS ufds(2*n);
    while (q--) {
        char x;
        int a, b;
        cin >> x >> a >> b;
        --a, --b;
        if (x == 'Q') {
            if (ufds.same(a, b)) cout << 'R' << '\n';
            else if (ufds.same(a+n, b)) cout << 'A' << '\n';
            else cout << '?' << '\n';
        } else {
            if (x == 'R') {
                ufds.unite(a, b);
                ufds.unite(a+n, b+n);
            } else {
                ufds.unite(a+n, b);
                ufds.unite(a, b+n);
            }
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 2028 KB Output is correct
2 Correct 24 ms 1900 KB Output is correct
3 Correct 35 ms 1900 KB Output is correct
4 Correct 24 ms 1900 KB Output is correct
5 Correct 27 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2028 KB Output is correct
2 Correct 26 ms 2028 KB Output is correct
3 Correct 38 ms 1900 KB Output is correct
4 Correct 30 ms 1900 KB Output is correct
5 Correct 33 ms 2012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1900 KB Output is correct
2 Correct 27 ms 1900 KB Output is correct
3 Correct 35 ms 1772 KB Output is correct
4 Correct 35 ms 1900 KB Output is correct
5 Correct 30 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 34 ms 2028 KB Output is correct
7 Correct 24 ms 1900 KB Output is correct
8 Correct 35 ms 1900 KB Output is correct
9 Correct 24 ms 1900 KB Output is correct
10 Correct 27 ms 1900 KB Output is correct
11 Correct 28 ms 2028 KB Output is correct
12 Correct 26 ms 2028 KB Output is correct
13 Correct 38 ms 1900 KB Output is correct
14 Correct 30 ms 1900 KB Output is correct
15 Correct 33 ms 2012 KB Output is correct
16 Correct 25 ms 1900 KB Output is correct
17 Correct 27 ms 1900 KB Output is correct
18 Correct 35 ms 1772 KB Output is correct
19 Correct 35 ms 1900 KB Output is correct
20 Correct 30 ms 1900 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 27 ms 3308 KB Output is correct
27 Correct 36 ms 3308 KB Output is correct
28 Correct 37 ms 3052 KB Output is correct
29 Correct 29 ms 3052 KB Output is correct
30 Correct 30 ms 3308 KB Output is correct