Submission #328270

# Submission time Handle Problem Language Result Execution time Memory
328270 2020-11-16T04:09:23 Z arujbansal Experimental Charges (NOI19_charges) C++17
100 / 100
35 ms 3180 KB
#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
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 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 23 ms 2412 KB Output is correct
2 Correct 24 ms 2412 KB Output is correct
3 Correct 29 ms 2412 KB Output is correct
4 Correct 25 ms 2540 KB Output is correct
5 Correct 24 ms 2412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2924 KB Output is correct
2 Correct 30 ms 2924 KB Output is correct
3 Correct 29 ms 2924 KB Output is correct
4 Correct 34 ms 2924 KB Output is correct
5 Correct 35 ms 3052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2796 KB Output is correct
2 Correct 29 ms 3180 KB Output is correct
3 Correct 30 ms 2668 KB Output is correct
4 Correct 32 ms 2924 KB Output is correct
5 Correct 30 ms 2796 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 1 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 1 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 23 ms 2412 KB Output is correct
7 Correct 24 ms 2412 KB Output is correct
8 Correct 29 ms 2412 KB Output is correct
9 Correct 25 ms 2540 KB Output is correct
10 Correct 24 ms 2412 KB Output is correct
11 Correct 27 ms 2924 KB Output is correct
12 Correct 30 ms 2924 KB Output is correct
13 Correct 29 ms 2924 KB Output is correct
14 Correct 34 ms 2924 KB Output is correct
15 Correct 35 ms 3052 KB Output is correct
16 Correct 27 ms 2796 KB Output is correct
17 Correct 29 ms 3180 KB Output is correct
18 Correct 30 ms 2668 KB Output is correct
19 Correct 32 ms 2924 KB Output is correct
20 Correct 30 ms 2796 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 32 ms 2852 KB Output is correct
27 Correct 31 ms 2924 KB Output is correct
28 Correct 30 ms 2796 KB Output is correct
29 Correct 28 ms 2924 KB Output is correct
30 Correct 28 ms 2816 KB Output is correct