답안 #520366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520366 2022-01-29T16:32:54 Z Cyanmond 낙하산 고리들 (IOI12_rings) C++17
100 / 100
1091 ms 95736 KB
#line 1 "paper.cpp"
#include <bits/stdc++.h>

#line 3 "library2/utility/len.hpp"

template <class Container> int len(const Container&c){
    return static_cast<int>(std::size(c));
}
#line 7 "library2/graph/union_find.hpp"

class UnionFind {
    int components;
    std::vector<int> data;

  public:
    explicit UnionFind() : components(0), data(0) {}
    explicit UnionFind(const int n) : components(n), data(n, -1) {}

    int size() const {
        return len(data);
    }

    int count_components() const {
        return components;
    }

    int find(int v) {
        assert(0 <= v and v < size());
        if (data[v] < 0) {
            return v;
        } else {
            return data[v] = find(data[v]);
        }
    }

    int size(const int v) {
        return -data[find(v)];
    }

    bool is_same(const int a, const int b) {
        return find(a) == find(b);
    }

    bool unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) {
            return false;
        }
        if (size(a) < size(b)) {
            std::swap(a, b);
        }
        data[a] += data[b];
        data[b] = a;
        return true;
    }

    std::vector<std::vector<int>> decompose() {
        std::vector<std::vector<int>> ret(size());
        for (int i = 0; i < size(); ++i) {
            ret[find(i)].push_back(i);
        }
        ret.erase(std::remove_if(ret.begin(), ret.end(),
                                 [&](const std::vector<int> &v) { return v.empty(); }),
                  ret.end());
        return ret;
    }
};
#line 3 "library2/utility/int_alias.hpp"

using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
#line 3 "library2/utility/rep.hpp"

class Range {
    struct Iterator {
        int itr;
        constexpr Iterator(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept {
            ++itr;
        }
        constexpr bool operator!=(const Iterator &other) const noexcept {
            return itr != other.itr;
        }
        constexpr int operator*() const noexcept {
            return itr;
        }
    };
    const Iterator first, last;

  public:
    explicit constexpr Range(const int f, const int l) noexcept
        : first(f), last(std::max(f, l)) {}
    constexpr Iterator begin() const noexcept {
        return first;
    }
    constexpr Iterator end() const noexcept {
        return last;
    }
};

constexpr Range rep(const int l, const int r) noexcept {
    return Range(l, r);
}
constexpr Range rep(const int n) noexcept {
    return Range(0, n);
}
#line 3 "library2/utility/scan.hpp"

template <typename T = int> T scan() {
    T ret;
    std::cin >> ret;
    return ret;
}
#line 8 "paper.cpp"

int N;
bool impossible;
bool cyclet;
bool started;
bool fn;
UnionFind uft;

struct Edge {
    int u;
    int v;
};

std::vector<Edge> edges;
std::vector<std::vector<int>> graph;

int a, b, c, d;
UnionFind ufta, uftb, uftc, uftd;
bool oka, okb, okc, okd;

int cycle_size;

void add_edge(const int u, const int v) {
    if (not started) {
        return;
    }
    if (oka and a != u and a != v) {
        if (not ufta.unite(u, v)) {
            oka = false;
        }
    }
    if (okb and b != u and b != v) {
        if (not uftb.unite(u, v)) {
            okb = false;
        }
    }
    if (okc and c != u and c != v) {
        if (not uftc.unite(u, v)) {
            okc = false;
        }
    }
    if (okd and d != u and d != v) {
        if (not uftd.unite(u, v)) {
            okd = false;
        }
    }
}

void set(const int x, const int y, const int z, const int p) {
    a = x;
    b = y;
    c = z;
    d = p;
    oka = okb = okc = okd = true;
    ufta = uftb = uftc = uftd = UnionFind(N);
    for (const auto &[u, v] : edges) {
        add_edge(u, v);
    }
}

int count() {
    return (oka ? 1 : 0) + (okb ? 1 : 0) + (okc ? 1 : 0) + (okd ? 1 : 0);
}

void init(const int n) {
    N = n;
    impossible = false;
    cyclet = false;
    started = false;
    fn = false;
    cycle_size = N;
    uft = UnionFind(N);
    graph.resize(n);
}

int degree(const int v) {
    return len(graph[v]);
}

void start(const int n) {
    started = true;
    set(n, graph[n][0], graph[n][1], graph[n][2]);
}

void add_degree3(const int n) {
    auto is_ok = [&](const int v) {
        return (v == n) or (std::find(graph[n].begin(), graph[n].end(), v) != graph[n].end());
    };
    if (not is_ok(a)) {
        oka = false;
    }
    if (not is_ok(b)) {
        okb = false;
    }
    if (not is_ok(c)) {
        okc = false;
    }
    if (not is_ok(d)) {
        okd = false;
    }
}

void last_spart(const int n) {
    fn = true;
    if (n != a) {
        oka = false;
    }
    if (n != b) {
        okb = false;
    }
    if (n != c) {
        okc = false;
    }
    if (n != d) {
        okd = false;
    }
}

void link(const int u, const int v) {
    if (not impossible) {
        add_edge(u, v);
        const auto h = uft.unite(u, v);
        graph[u].push_back(v);
        graph[v].push_back(u);
        edges.push_back({u, v});

        const int da = degree(u), db = degree(v);
        if (da == 3) {
            if (not started) {
                start(u);
            } else {
                add_degree3(u);
            }
        }
        if (db == 3) {
            if (not started) {
                start(v);
            } else {
                add_degree3(v);
            }
        }
        if (da == 4) {
            last_spart(u);
        }
        if (db == 4) {
            last_spart(v);
        }

        if (not started) {
            if (not h) {
                if (not cyclet) {
                    cyclet = true;
                    cycle_size = uft.size(u);
                } else {
                    impossible = true;
                }
            }
        }
    }
}

int count_critical() {
    if (impossible) {
        return 0;
    }
    if (not started) {
        return cycle_size;
    }
    return count();
}

void Init(int n) {
    init(n);
}

void Link(int A, int B) {
    link(A, B);
}

int CountCritical() {
    return count_critical();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 ms 720 KB Output is correct
7 Correct 1 ms 560 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 3 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 34912 KB Output is correct
2 Correct 712 ms 66032 KB Output is correct
3 Correct 764 ms 77996 KB Output is correct
4 Correct 785 ms 66748 KB Output is correct
5 Correct 848 ms 80208 KB Output is correct
6 Correct 768 ms 78400 KB Output is correct
7 Correct 724 ms 89288 KB Output is correct
8 Correct 1010 ms 89736 KB Output is correct
9 Correct 1091 ms 95736 KB Output is correct
10 Correct 541 ms 77168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 ms 720 KB Output is correct
7 Correct 1 ms 560 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 3 ms 720 KB Output is correct
11 Correct 2 ms 732 KB Output is correct
12 Correct 4 ms 1292 KB Output is correct
13 Correct 4 ms 1232 KB Output is correct
14 Correct 3 ms 996 KB Output is correct
15 Correct 3 ms 1488 KB Output is correct
16 Correct 4 ms 1104 KB Output is correct
17 Correct 5 ms 1232 KB Output is correct
18 Correct 5 ms 1744 KB Output is correct
19 Correct 4 ms 1104 KB Output is correct
20 Correct 5 ms 1232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 ms 720 KB Output is correct
7 Correct 1 ms 560 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 3 ms 720 KB Output is correct
11 Correct 2 ms 732 KB Output is correct
12 Correct 4 ms 1292 KB Output is correct
13 Correct 4 ms 1232 KB Output is correct
14 Correct 3 ms 996 KB Output is correct
15 Correct 3 ms 1488 KB Output is correct
16 Correct 4 ms 1104 KB Output is correct
17 Correct 5 ms 1232 KB Output is correct
18 Correct 5 ms 1744 KB Output is correct
19 Correct 4 ms 1104 KB Output is correct
20 Correct 5 ms 1232 KB Output is correct
21 Correct 15 ms 3216 KB Output is correct
22 Correct 24 ms 4928 KB Output is correct
23 Correct 29 ms 6292 KB Output is correct
24 Correct 34 ms 6980 KB Output is correct
25 Correct 13 ms 5072 KB Output is correct
26 Correct 33 ms 7884 KB Output is correct
27 Correct 33 ms 6828 KB Output is correct
28 Correct 35 ms 8556 KB Output is correct
29 Correct 27 ms 7028 KB Output is correct
30 Correct 42 ms 7920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 448 KB Output is correct
6 Correct 2 ms 720 KB Output is correct
7 Correct 1 ms 560 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 732 KB Output is correct
10 Correct 3 ms 720 KB Output is correct
11 Correct 291 ms 34912 KB Output is correct
12 Correct 712 ms 66032 KB Output is correct
13 Correct 764 ms 77996 KB Output is correct
14 Correct 785 ms 66748 KB Output is correct
15 Correct 848 ms 80208 KB Output is correct
16 Correct 768 ms 78400 KB Output is correct
17 Correct 724 ms 89288 KB Output is correct
18 Correct 1010 ms 89736 KB Output is correct
19 Correct 1091 ms 95736 KB Output is correct
20 Correct 541 ms 77168 KB Output is correct
21 Correct 2 ms 732 KB Output is correct
22 Correct 4 ms 1292 KB Output is correct
23 Correct 4 ms 1232 KB Output is correct
24 Correct 3 ms 996 KB Output is correct
25 Correct 3 ms 1488 KB Output is correct
26 Correct 4 ms 1104 KB Output is correct
27 Correct 5 ms 1232 KB Output is correct
28 Correct 5 ms 1744 KB Output is correct
29 Correct 4 ms 1104 KB Output is correct
30 Correct 5 ms 1232 KB Output is correct
31 Correct 15 ms 3216 KB Output is correct
32 Correct 24 ms 4928 KB Output is correct
33 Correct 29 ms 6292 KB Output is correct
34 Correct 34 ms 6980 KB Output is correct
35 Correct 13 ms 5072 KB Output is correct
36 Correct 33 ms 7884 KB Output is correct
37 Correct 33 ms 6828 KB Output is correct
38 Correct 35 ms 8556 KB Output is correct
39 Correct 27 ms 7028 KB Output is correct
40 Correct 42 ms 7920 KB Output is correct
41 Correct 164 ms 27700 KB Output is correct
42 Correct 477 ms 62564 KB Output is correct
43 Correct 225 ms 47676 KB Output is correct
44 Correct 531 ms 87120 KB Output is correct
45 Correct 582 ms 78612 KB Output is correct
46 Correct 509 ms 66468 KB Output is correct
47 Correct 692 ms 68380 KB Output is correct
48 Correct 383 ms 68808 KB Output is correct
49 Correct 512 ms 69264 KB Output is correct
50 Correct 552 ms 68440 KB Output is correct
51 Correct 239 ms 44304 KB Output is correct
52 Correct 477 ms 70808 KB Output is correct
53 Correct 401 ms 69320 KB Output is correct
54 Correct 891 ms 78136 KB Output is correct
55 Correct 799 ms 84860 KB Output is correct