This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |