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);
}
}
}
};
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;
}
# | 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... |