답안 #674730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674730 2022-12-26T02:31:36 Z vuavisao Experimental Charges (NOI19_charges) C++14
100 / 100
50 ms 8388 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

template<typename Lhs, typename Rhs> inline void Max_self(Lhs &a, Rhs b) { a = (a > b ? a : b); }
template<typename Lhs, typename Rhs> inline void Min_self(Lhs &a, Rhs b) { a = (a < b ? a : b); }

const int N = 1e5 + 10;
const char Res[] = {'A', 'R', '?'};

struct dsu {
    vector<int> parent = {};

    void resize(int _n) { parent.resize(_n + 10); for(int i = 1; i <= _n; ++ i) parent[i] = i; }

    dsu() {};
    dsu(int n) { this -> resize(n); }

    int ancestor(int u) {
        if(parent[u] == u) return u;
        return parent[u] = ancestor(parent[u]);
    }

    void join(int u, int v) {
        u = ancestor(u), v = ancestor(v);
        if(u == v) return;
        parent[v] = u;
    }
};

struct state {
    int v = 0;
    int mask = 0;
    state() {};
    state(int _v, int _mask) { v = _v; mask = _mask; };
};

int n, q;
vector<state> g[N];
char qq[N];
pair<int, int> couple[N];

bool visited[N];
bool have[N];

void dfs(int u) {
    visited[u] = true;
    for(const auto& psy : g[u]) {
        int v = psy.v, mask = psy.mask;
        if(visited[v]) continue;
        visited[v] = true;
        have[v] = have[u] ^ mask;
        dfs(v);
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("NOI19_charges.inp", "r")) {
        freopen("NOI19_charges.inp", "r", stdin);
        freopen("NOI19_charges.out", "w", stdout);
    }
    cin >> n >> q;
    for(int i = 1; i <= q; ++ i) {
        int u, v; cin >> qq[i] >> u >> v;
        couple[i] = make_pair(u, v);
        if(qq[i] == 'Q') continue;
        int mask = 0;
        if(qq[i] == 'A') mask = 1;
        g[u].push_back(state(v, mask));
        g[v].push_back(state(u, mask));
    }
    for(int i = 1; i <= n; ++ i) {
        if(visited[i]) continue;
        dfs(i);
    }
    dsu dsu(n);
    for(int i = 1; i <= q; ++ i) {
        const auto& psy = couple[i];
        int u = psy.first, v = psy.second;
        if(qq[i] != 'Q') {
            dsu.join(u, v);
        }
        else {
            int res = 2;
            if(dsu.ancestor(u) == dsu.ancestor(v)) {
                res = (have[u] == have[v]);
            }
            cout << Res[res] << '\n';
        }
    }
    return 0;
}

/// Code by vuavisao

Compilation message

charges.cpp: In function 'int32_t main()':
charges.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen("NOI19_charges.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
charges.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen("NOI19_charges.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2672 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 7104 KB Output is correct
2 Correct 25 ms 7424 KB Output is correct
3 Correct 34 ms 6540 KB Output is correct
4 Correct 26 ms 7168 KB Output is correct
5 Correct 26 ms 7288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8388 KB Output is correct
2 Correct 33 ms 7804 KB Output is correct
3 Correct 37 ms 7164 KB Output is correct
4 Correct 37 ms 8144 KB Output is correct
5 Correct 40 ms 7736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 8268 KB Output is correct
2 Correct 29 ms 7764 KB Output is correct
3 Correct 34 ms 7624 KB Output is correct
4 Correct 43 ms 8044 KB Output is correct
5 Correct 42 ms 8040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2672 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2692 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 3 ms 2696 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2672 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 25 ms 7104 KB Output is correct
7 Correct 25 ms 7424 KB Output is correct
8 Correct 34 ms 6540 KB Output is correct
9 Correct 26 ms 7168 KB Output is correct
10 Correct 26 ms 7288 KB Output is correct
11 Correct 26 ms 8388 KB Output is correct
12 Correct 33 ms 7804 KB Output is correct
13 Correct 37 ms 7164 KB Output is correct
14 Correct 37 ms 8144 KB Output is correct
15 Correct 40 ms 7736 KB Output is correct
16 Correct 25 ms 8268 KB Output is correct
17 Correct 29 ms 7764 KB Output is correct
18 Correct 34 ms 7624 KB Output is correct
19 Correct 43 ms 8044 KB Output is correct
20 Correct 42 ms 8040 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2692 KB Output is correct
23 Correct 3 ms 2644 KB Output is correct
24 Correct 3 ms 2696 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 26 ms 8188 KB Output is correct
27 Correct 33 ms 7720 KB Output is correct
28 Correct 38 ms 7892 KB Output is correct
29 Correct 35 ms 7780 KB Output is correct
30 Correct 50 ms 7860 KB Output is correct