Submission #663244

# Submission time Handle Problem Language Result Execution time Memory
663244 2022-11-26T17:03:14 Z arujbansal Experimental Charges (NOI19_charges) C++17
100 / 100
28 ms 2504 KB
#include <bits/stdc++.h>

using namespace std;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

using ll = long long;

template<typename S, typename T> void smax(S &a, const T &b) { if (a < b) a = b; }
template<typename S, typename T> void smin(S &a, const T &b) { if (a > b) a = b; }

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long

const int MXN = 1e5 + 5, INF = 1e9 + 5;

struct UFDS {
    int n;
    vector<int> info;
 
    UFDS() {}
 
    UFDS(int _n) { init(_n); }
 
    void init(int _n) {
        n = _n;
        info.assign(n, -1);
    }
 
    int get(int x) {
        if (info[x] < 0) return x;
        return info[x] = get(info[x]);
    }
 
    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x == y) return false;
 
        if (info[x] > info[y])
            swap(x, y);
 
        info[x] += info[y];
        info[y] = x;
 
        return true;    
    }
 
    bool connected(int x, int y) { return get(x) == get(y); }
};
 
struct two_colouring {
    int n;
    UFDS ufds;
    vector<int> answer, in_answer;
 
    two_colouring(int _n) { init(_n); }
 
    void init(int _n) {
        n = _n;
        ufds.init(2 * n);
    }
 
    void add_clause_equal(int x, int y) {
        ufds.unite(x, y);
        ufds.unite(x + n, y + n);
    }
 
    void add_clause_opposite(int x, int y) {
        ufds.unite(x, y + n);
        ufds.unite(x + n, y);
    }
 
    bool satisfiable() {
        for (int i = 0; i < n; i++)
            if (ufds.connected(i, i + n)) return false;
 
        return true;
    }
    
    // Construct an answer where we minimise the number of "which" values in each component
    // which is either 1 or 0
    void construct_answer(int which) {
        answer.resize(n);
        in_answer.assign(2 * n, 0);
        vector<int> cnt(2 * n, 0);

        for (int i = 0; i < 2 * n; i++) 
            cnt[ufds.get(i)] += (i > n) ^ which;

        for (int i = 0; i < n; i++) {
            int cur = ufds.get(i), complement = ufds.get(i + n);

            if (in_answer[cur] || in_answer[complement])
                continue;

            if (cnt[cur] < cnt[complement])
                in_answer[cur] = true;
            else 
                in_answer[complement] = true;
        }

        for (int i = 0; i < n; i++)
            answer[i] = (in_answer[ufds.get(i)] ? 1 : 0);
    }
};

void solve() {
    int N, Q;
    cin >> N >> Q;

    two_colouring solver(N);
    while (Q--) {
        char t;
        int a, b;
        cin >> t >> a >> b;
        a--, b--;

        if (t == 'A') solver.add_clause_opposite(a, b);
        else if (t == 'R') solver.add_clause_equal(a, b);
        else {
            if (solver.ufds.connected(a, b)) cout << "R\n";
            else if (solver.ufds.connected(a, b + N)) cout << "A\n";
            else cout << "?\n"; 
        }   
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int TC = 1;
    // cin >> TC;
    while (TC--) solve();
}
# 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2004 KB Output is correct
2 Correct 19 ms 1952 KB Output is correct
3 Correct 19 ms 2004 KB Output is correct
4 Correct 19 ms 1996 KB Output is correct
5 Correct 20 ms 1992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2504 KB Output is correct
2 Correct 19 ms 2372 KB Output is correct
3 Correct 22 ms 2480 KB Output is correct
4 Correct 28 ms 2380 KB Output is correct
5 Correct 22 ms 2440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2340 KB Output is correct
2 Correct 20 ms 2360 KB Output is correct
3 Correct 20 ms 2312 KB Output is correct
4 Correct 24 ms 2484 KB Output is correct
5 Correct 21 ms 2380 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 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 212 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 16 ms 2004 KB Output is correct
7 Correct 19 ms 1952 KB Output is correct
8 Correct 19 ms 2004 KB Output is correct
9 Correct 19 ms 1996 KB Output is correct
10 Correct 20 ms 1992 KB Output is correct
11 Correct 27 ms 2504 KB Output is correct
12 Correct 19 ms 2372 KB Output is correct
13 Correct 22 ms 2480 KB Output is correct
14 Correct 28 ms 2380 KB Output is correct
15 Correct 22 ms 2440 KB Output is correct
16 Correct 23 ms 2340 KB Output is correct
17 Correct 20 ms 2360 KB Output is correct
18 Correct 20 ms 2312 KB Output is correct
19 Correct 24 ms 2484 KB Output is correct
20 Correct 21 ms 2380 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 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 19 ms 2388 KB Output is correct
27 Correct 20 ms 2356 KB Output is correct
28 Correct 22 ms 2388 KB Output is correct
29 Correct 20 ms 2260 KB Output is correct
30 Correct 19 ms 2372 KB Output is correct