답안 #396538

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396538 2021-04-30T08:59:12 Z KoD Viruses (BOI20_viruses) C++17
100 / 100
77 ms 3560 KB
#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

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

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T> bool setmin(T& lhs, const T& rhs) {
    if (lhs > rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T> using Vec = std::vector<T>;

struct Trie {
    struct Node {
        bool bad;
        usize link;
        std::array<usize, 2> next;
        Node() : bad(false), link(-1) { next.fill(-1); }
    };

    Vec<Node> node;
    usize add_node() {
        node.push_back(Node());
        return node.size() - 1;
    }

    Trie() { add_node(); }

    void insert(const Vec<usize>& binary) {
        usize pos = 0;
        for (const auto x : binary) {
            if (node[pos].next[x] == (usize)-1) {
                node[pos].next[x] = add_node();
            }
            pos = node[pos].next[x];
        }
        node[pos].bad = true;
    }

    void build() {
        std::queue<usize> que;
        for (const auto k : rep(0, 2)) {
            if (node[0].next[k] == (usize)-1) {
                node[0].next[k] = add_node();
            }
            node[node[0].next[k]].link = 0;
            que.push(node[0].next[k]);
        }
        while (!que.empty()) {
            const auto u = que.front();
            que.pop();
            for (const auto k : rep(0, 2)) {
                const auto v = node[u].next[k];
                if (v == (usize)-1) {
                    continue;
                }
                usize p = node[u].link;
                while (node[p].next[k] == (usize)-1) {
                    p = node[p].link;
                }
                node[v].link = node[p].next[k];
                if (node[node[v].link].bad) {
                    node[v].bad = true;
                }
                que.push(v);
            }
        }
    }
};

struct Edge {
    Vec<usize> info;
    // info[0]: type
    // type 0: a -> this
    // type 1: a -> this, other
    // type 2: a -> other, this
};

struct State {
    u64 d;
    usize k, u, v;
    bool operator<(const State& other) const { return d < other.d; }
    bool operator>(const State& other) const { return d > other.d; }
};

template <class T> using Heap = std::priority_queue<T, Vec<T>, std::greater<T>>;

void BOI20_Viruses_main() {
    usize G, N, M;
    std::cin >> G >> N >> M;
    Vec<Vec<Edge>> graph(G);
    const auto add_gene = [&] {
        graph.push_back({});
        return graph.size() - 1;
    };
    while (N--) {
        usize a;
        std::cin >> a;
        usize k;
        std::cin >> k;
        Vec<usize> b(k);
        for (auto& x : b) {
            std::cin >> x;
        }
        while (b.size() > 2) {
            const auto c = add_gene();
            graph[c].push_back({{1, a, b.back()}});
            graph[b.back()].push_back({{2, a, c}});
            b.pop_back();
            a = c;
        }
        if (b.size() == 2) {
            graph[b[0]].push_back({{1, a, b[1]}});
            graph[b[1]].push_back({{2, a, b[0]}});
        } else {
            graph[b[0]].push_back({{0, a}});
        }
    }
    Trie trie;
    while (M--) {
        usize c;
        std::cin >> c;
        Vec<usize> b(c);
        for (auto& x : b) {
            std::cin >> x;
        }
        trie.insert(b);
    }
    trie.build();
    const auto V = trie.node.size();
    const auto G2 = graph.size();
    Vec<Vec<Vec<u64>>> dist(G2, Vec<Vec<u64>>(V, Vec<u64>(V, INFTY<u64, 1>)));
    Heap<State> heap;
    const auto push = [&](const usize k, const usize u, const usize v,
                          const u64 d) {
        if (setmin(dist[k][u][v], d)) {
            heap.push(State{d, k, u, v});
        }
    };
    for (const auto k : rep(0, 2)) {
        for (const auto u : rep(0, V)) {
            usize v = u;
            while (trie.node[v].next[k] == (usize)-1) {
                v = trie.node[v].link;
            }
            v = trie.node[v].next[k];
            if (!trie.node[v].bad) {
                push(k, u, v, 1);
            }
        }
    }
    while (!heap.empty()) {
        const auto [d, k, u, v] = heap.top();
        heap.pop();
        if (dist[k][u][v] < d) {
            continue;
        }
        for (const auto& e : graph[k]) {
            if (e.info[0] == 0) {
                push(e.info[1], u, v, d);
            } else if (e.info[0] == 1) {
                for (const auto w : rep(0, V)) {
                    const auto d2 = dist[e.info[2]][v][w];
                    if (d2 != INFTY<u64, 1>) {
                        push(e.info[1], u, w, d + d2);
                    }
                }
            } else {
                for (const auto w : rep(0, V)) {
                    const auto d2 = dist[e.info[2]][w][u];
                    if (d2 != INFTY<u64, 1>) {
                        push(e.info[1], w, v, d + d2);
                    }
                }
            }
        }
    }
    for (const auto k : rep(2, G)) {
        u64 ans = INFTY<u64, 1>;
        for (const auto u : rep(0, V)) {
            if (!trie.node[u].bad) {
                setmin(ans, dist[k][0][u]);
            }
        }
        if (ans == INFTY<u64, 1>) {
            std::cout << "YES\n";
        } else {
            std::cout << "NO " << ans << "\n";
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    BOI20_Viruses_main();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 1484 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 1484 KB Output is correct
6 Correct 2 ms 1356 KB Output is correct
7 Correct 3 ms 1740 KB Output is correct
8 Correct 4 ms 1868 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 444 KB Output is correct
13 Correct 1 ms 972 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 716 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 2 ms 708 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1484 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 2 ms 1484 KB Output is correct
4 Correct 2 ms 1356 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 4 ms 1868 KB Output is correct
7 Correct 2 ms 1100 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 4 ms 972 KB Output is correct
26 Correct 1 ms 1484 KB Output is correct
27 Correct 55 ms 1868 KB Output is correct
28 Correct 4 ms 1868 KB Output is correct
29 Correct 64 ms 3412 KB Output is correct
30 Correct 58 ms 3400 KB Output is correct
31 Correct 7 ms 1996 KB Output is correct
32 Correct 5 ms 1996 KB Output is correct
33 Correct 2 ms 1484 KB Output is correct
34 Correct 57 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 312 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 316 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 2 ms 1484 KB Output is correct
22 Correct 3 ms 2636 KB Output is correct
23 Correct 2 ms 1484 KB Output is correct
24 Correct 2 ms 1356 KB Output is correct
25 Correct 3 ms 1740 KB Output is correct
26 Correct 4 ms 1868 KB Output is correct
27 Correct 2 ms 1100 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 592 KB Output is correct
30 Correct 1 ms 444 KB Output is correct
31 Correct 1 ms 972 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 1 ms 716 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 716 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 1 ms 716 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 2 ms 708 KB Output is correct
40 Correct 1 ms 460 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 320 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 312 KB Output is correct
47 Correct 1 ms 316 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 336 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 316 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 4 ms 972 KB Output is correct
61 Correct 1 ms 1484 KB Output is correct
62 Correct 55 ms 1868 KB Output is correct
63 Correct 4 ms 1868 KB Output is correct
64 Correct 64 ms 3412 KB Output is correct
65 Correct 58 ms 3400 KB Output is correct
66 Correct 7 ms 1996 KB Output is correct
67 Correct 5 ms 1996 KB Output is correct
68 Correct 2 ms 1484 KB Output is correct
69 Correct 57 ms 2392 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 332 KB Output is correct
75 Correct 1 ms 332 KB Output is correct
76 Correct 1 ms 332 KB Output is correct
77 Correct 1 ms 332 KB Output is correct
78 Correct 1 ms 336 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 1 ms 332 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 332 KB Output is correct
86 Correct 1 ms 332 KB Output is correct
87 Correct 8 ms 1100 KB Output is correct
88 Correct 1 ms 332 KB Output is correct
89 Correct 1 ms 332 KB Output is correct
90 Correct 1 ms 588 KB Output is correct
91 Correct 2 ms 440 KB Output is correct
92 Correct 35 ms 3148 KB Output is correct
93 Correct 20 ms 3016 KB Output is correct
94 Correct 1 ms 972 KB Output is correct
95 Correct 1 ms 716 KB Output is correct
96 Correct 1 ms 844 KB Output is correct
97 Correct 7 ms 1488 KB Output is correct
98 Correct 2 ms 1100 KB Output is correct
99 Correct 10 ms 1740 KB Output is correct
100 Correct 2 ms 972 KB Output is correct
101 Correct 3 ms 844 KB Output is correct
102 Correct 77 ms 3560 KB Output is correct
103 Correct 3 ms 1484 KB Output is correct
104 Correct 14 ms 1868 KB Output is correct
105 Correct 1 ms 460 KB Output is correct
106 Correct 23 ms 2264 KB Output is correct
107 Correct 22 ms 2264 KB Output is correct