Submission #971367

# Submission time Handle Problem Language Result Execution time Memory
971367 2024-04-28T12:07:05 Z vjudge1 Experimental Charges (NOI19_charges) C++14
100 / 100
21 ms 3420 KB
/*
 * Created at 1:32 PM on 30 Mar 2024
 */

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <numeric>
#include <cassert>
#include <functional>

#ifdef LOCAL
#include "debug.h"
#else
#define trace(...) 42
#endif

struct DSU {
private:
    std::size_t n;
    std::vector<size_t> link;
    std::vector<int> size;

    std::vector<int> flipped;

public:
    explicit DSU(const size_t n_) : n(n_),
                                    size(n_, 1), link(n_),
                                    flipped(n_, 0) {
        std::iota(link.begin(), link.end(), 0);
    }

    std::size_t find(std::size_t x) const {
        while (x != link[x]) {
            x = link[x];
        }
        return x;
    }

    int find_charge(std::size_t x) const {
        auto res = flipped[x];
        while (x != link[x]) {
            x = link[x];
            res ^= flipped[x];
        }
        return res;
    }

    enum class InteractionType {
        attract,
        repel,
        uncertain
    };

    bool unite(std::size_t x, std::size_t y, const InteractionType it) {
        assert(it != InteractionType::uncertain);

        bool flip = it == InteractionType::attract and find_charge(x) == find_charge(y);
        flip |= it == InteractionType::repel and find_charge(x) != find_charge(y);

        x = find(x);
        y = find(y);

        if (x == y) {
            assert(!flip);
            return false;
        }

        if (size[x] < size[y]) {
            std::swap(x, y);
        }

        trace(x, y, flip);

        flipped[y] ^= flip; // TODO: checks?
        link[y] = x;
        size[x] += size[y];

        return true;
    }

    InteractionType interact(const std::size_t x, const std::size_t y) const {
        if (find(x) != find(y)) {
            return InteractionType::uncertain;
        }

        return find_charge(x) != find_charge(y) ? InteractionType::attract : InteractionType::repel;
    }
};

int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::size_t n, q;
    std::cin >> n >> q;

    DSU dsu(n);

    while (q--) {
        char op;
        std::cin >> op;

        std::size_t a, b;
        std::cin >> a >> b;
        --a, --b;

        if (op == 'A') {
            dsu.unite(a, b, DSU::InteractionType::attract);
        } else if (op == 'R') {
            dsu.unite(a, b, DSU::InteractionType::repel);
        } else if (op == 'Q') {
            char ans;

            switch (dsu.interact(a, b)) {
                case DSU::InteractionType::attract:
                    ans = 'A';
                    break;
                case DSU::InteractionType::repel:
                    ans = 'R';
                    break;
                case DSU::InteractionType::uncertain:
                    ans = '?';
                    break;
            }

            std::cout << ans << '\n';
        }
    }
    return 0;
}

Compilation message

charges.cpp: In constructor 'DSU::DSU(size_t)':
charges.cpp:26:22: warning: 'DSU::size' will be initialized after [-Wreorder]
   26 |     std::vector<int> size;
      |                      ^~~~
charges.cpp:25:25: warning:   'std::vector<long unsigned int> DSU::link' [-Wreorder]
   25 |     std::vector<size_t> link;
      |                         ^~~~
charges.cpp:31:14: warning:   when initialized here [-Wreorder]
   31 |     explicit DSU(const size_t n_) : n(n_),
      |              ^~~
charges.cpp: In member function 'bool DSU::unite(std::size_t, std::size_t, DSU::InteractionType)':
charges.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define trace(...) 42
      |                    ^~
charges.cpp:77:9: note: in expansion of macro 'trace'
   77 |         trace(x, y, flip);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2860 KB Output is correct
2 Correct 12 ms 2908 KB Output is correct
3 Correct 14 ms 2904 KB Output is correct
4 Correct 14 ms 2908 KB Output is correct
5 Correct 13 ms 2904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 3312 KB Output is correct
2 Correct 15 ms 3160 KB Output is correct
3 Correct 17 ms 3376 KB Output is correct
4 Correct 17 ms 3160 KB Output is correct
5 Correct 17 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3160 KB Output is correct
2 Correct 15 ms 3164 KB Output is correct
3 Correct 21 ms 3164 KB Output is correct
4 Correct 17 ms 3164 KB Output is correct
5 Correct 16 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 13 ms 2860 KB Output is correct
7 Correct 12 ms 2908 KB Output is correct
8 Correct 14 ms 2904 KB Output is correct
9 Correct 14 ms 2908 KB Output is correct
10 Correct 13 ms 2904 KB Output is correct
11 Correct 15 ms 3312 KB Output is correct
12 Correct 15 ms 3160 KB Output is correct
13 Correct 17 ms 3376 KB Output is correct
14 Correct 17 ms 3160 KB Output is correct
15 Correct 17 ms 3420 KB Output is correct
16 Correct 14 ms 3160 KB Output is correct
17 Correct 15 ms 3164 KB Output is correct
18 Correct 21 ms 3164 KB Output is correct
19 Correct 17 ms 3164 KB Output is correct
20 Correct 16 ms 3164 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 15 ms 3164 KB Output is correct
27 Correct 16 ms 3164 KB Output is correct
28 Correct 16 ms 3160 KB Output is correct
29 Correct 16 ms 2984 KB Output is correct
30 Correct 16 ms 3096 KB Output is correct