답안 #396536

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396536 2021-04-30T08:28:56 Z KoD Viruses (BOI20_viruses) C++17
43 / 100
700 ms 2636 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);
            }
        }
    }
};

void BOI20_Viruses_main() {
    usize G, N, M;
    std::cin >> G >> N >> M;
    Vec<Vec<Vec<usize>>> 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[a].push_back({c, b.back()});
            b.pop_back();
            a = c;
        }
        graph[a].push_back(b);
    }
    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>)));
    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) {
                setmin(dist[k][u][v], (u64)1);
            }
        }
    }
    while (true) {
        bool change = false;
        for (const auto k : rep(0, G2)) {
            for (const auto u : rep(0, V)) {
                for (const auto v : rep(0, V)) {
                    for (const auto& g : graph[k]) {
                        if (g.size() == 1) {
                            if (setmin(dist[k][u][v], dist[g[0]][u][v])) {
                                change = true;
                            }
                        } else {
                            for (const auto x : rep(0, V)) {
                                if (dist[g[0]][u][x] != INFTY<u64, 1> and
                                    dist[g[1]][x][v] != INFTY<u64, 1>) {
                                    if (setmin(dist[k][u][v],
                                               dist[g[0]][u][x] +
                                                   dist[g[1]][x][v])) {
                                        change = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!change) {
            break;
        }
    }
    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 312 KB Output is correct
3 Correct 1 ms 204 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 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 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 332 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 332 KB Output is correct
15 Correct 1 ms 316 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 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 30 ms 1464 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 28 ms 1484 KB Output is correct
6 Correct 309 ms 1428 KB Output is correct
7 Correct 550 ms 1740 KB Output is correct
8 Execution timed out 1072 ms 1972 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 1464 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 28 ms 1484 KB Output is correct
4 Correct 309 ms 1428 KB Output is correct
5 Correct 550 ms 1740 KB Output is correct
6 Execution timed out 1072 ms 1972 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 204 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 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 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 332 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 332 KB Output is correct
15 Correct 1 ms 316 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 204 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 204 KB Output is correct
22 Correct 1 ms 332 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 332 KB Output is correct
26 Correct 1 ms 332 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 2 ms 332 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 332 KB Output is correct
37 Correct 1 ms 320 KB Output is correct
38 Correct 3 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 208 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 2 ms 332 KB Output is correct
48 Correct 5 ms 332 KB Output is correct
49 Correct 2 ms 332 KB Output is correct
50 Correct 2 ms 332 KB Output is correct
51 Correct 2 ms 332 KB Output is correct
52 Correct 4 ms 332 KB Output is correct
53 Correct 2 ms 364 KB Output is correct
54 Correct 5 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 312 KB Output is correct
3 Correct 1 ms 204 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 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 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 332 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 332 KB Output is correct
15 Correct 1 ms 316 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 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 30 ms 1464 KB Output is correct
22 Correct 3 ms 2636 KB Output is correct
23 Correct 28 ms 1484 KB Output is correct
24 Correct 309 ms 1428 KB Output is correct
25 Correct 550 ms 1740 KB Output is correct
26 Execution timed out 1072 ms 1972 KB Time limit exceeded
27 Halted 0 ms 0 KB -