#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pliii tuple<ull, int, int, int>
#define F first
#define S second
using namespace std;
struct AhoCosarick
{
int child[55][2] = {{}}, fail[55] = {}, jump[55][2] = {{}}, idx = 0, has[55] = {};
void insert(int pos, vector<int> &s, int i = 0)
{
if (!child[i][s[pos]])
child[i][s[pos]] = ++idx;
if (pos + 1 < s.size())
insert(pos + 1, s, child[i][s[pos]]);
else
has[child[i][s[pos]]] = 1;
}
void build_fail()
{
fail[0] = -1;
queue<int> q;
q.push(0);
while (!q.empty())
{
int i = q.front();
q.pop();
for (int j = 0; j <= 1; j++)
{
if (child[i][j])
jump[i][j] = child[i][j];
else if (fail[i] >= 0)
jump[i][j] = jump[fail[i]][j];
}
if (fail[i] >= 0)
has[i] |= has[fail[i]];
// cout << i << (has[i] ? '*' : ' ') << " -> " << jump[i][0] << ' ' << jump[i][1] << '\n';
for (int j = 0; j <= 1; j++)
if (child[i][j])
{
int cur = fail[i];
while (cur >= 0)
{
if (child[cur][j])
break;
cur = fail[cur];
}
if (cur == -1)
fail[child[i][j]] = 0;
else
fail[child[i][j]] = child[cur][j];
q.push(child[i][j]);
}
}
}
} ac;
int G, N, M, pG;
const ull inf = 1LL << 63;
ull dp[205][55][55];
vector<tuple<int, int, int>> tr[205];
priority_queue<pliii, vector<pliii>, greater<pliii>> pq;
signed main()
{
cin >> G >> N >> M;
pG = G;
for (int i = 1; i <= N; i++)
{
int a, k;
cin >> a >> k;
vector<int> b(k);
for (int j = 0; j < k; j++)
cin >> b[j];
while (b.size() >= 3)
{
int y = b.back();
b.pop_back();
int x = b.back();
b.pop_back();
tr[x].emplace_back(make_tuple(G, y, 1));
tr[y].emplace_back(make_tuple(G, x, 2));
b.emplace_back(G);
G++;
}
if (b.size() == 1)
tr[b[0]].emplace_back(make_tuple(a, -1, 0));
else
{
tr[b[0]].emplace_back(make_tuple(a, b[1], 1));
tr[b[1]].emplace_back(make_tuple(a, b[0], 2));
}
}
for (int i = 1; i <= M; i++)
{
int l;
cin >> l;
vector<int> s(l);
for (int j = 0; j < l; j++)
cin >> s[j];
ac.insert(0, s);
}
ac.build_fail();
int L = ac.idx;
for (int i = 0; i < G; i++)
for (int j = 0; j <= L; j++)
for (int k = 0; k <= L; k++)
dp[i][j][k] = inf;
for (int i = 0; i <= L; i++)
{
if (!ac.has[ac.jump[i][0]])
{
dp[0][i][ac.jump[i][0]] = 1;
pq.push(make_tuple(1, 0, i, ac.jump[i][0]));
}
if (!ac.has[ac.jump[i][1]])
{
dp[1][i][ac.jump[i][1]] = 1;
pq.push(make_tuple(1, 1, i, ac.jump[i][1]));
}
}
while (!pq.empty())
{
auto [w, i, l, r] = pq.top();
pq.pop();
if (dp[i][l][r] < w)
continue;
for (auto [k, j, t] : tr[i])
if (t == 0)
{
if (dp[k][l][r] > dp[i][l][r])
{
dp[k][l][r] = dp[i][l][r];
pq.push(make_tuple(dp[k][l][r], k, l, r));
}
}
else if (t == 1)
{
for (int e = 0; e <= L; e++)
if (dp[k][l][e] > dp[i][l][r] + dp[j][r][e])
{
dp[k][l][e] = dp[i][l][r] + dp[j][r][e];
pq.push(make_tuple(dp[k][l][e], k, l, e));
}
}
else if (t == 2)
{
for (int s = 0; s <= L; s++)
if (dp[k][s][r] > dp[j][s][l] + dp[i][l][r])
{
dp[k][s][r] = dp[j][s][l] + dp[i][l][r];
pq.push(make_tuple(dp[k][s][r], k, s, r));
}
}
}
for (int i = 2; i < pG; i++)
{
ull ans = inf;
for (int j = 0; j <= L; j++)
ans = min(ans, dp[i][0][j]);
if (ans >= inf)
cout << "YES\n";
else
cout << "NO " << ans << '\n';
}
}
Compilation message
Viruses.cpp: In member function 'void AhoCosarick::insert(int, std::vector<int>&, int)':
Viruses.cpp:18:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | if (pos + 1 < s.size())
| ~~~~~~~~^~~~~~~~~~
Viruses.cpp: In function 'int main()':
Viruses.cpp:130:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
130 | auto [w, i, l, r] = pq.top();
| ^
Viruses.cpp:134:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
134 | for (auto [k, j, t] : tr[i])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
696 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
576 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
696 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1504 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
2080 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
952 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
2 ms |
1504 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
2080 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
564 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
5 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1464 KB |
Output is correct |
27 |
Correct |
33 ms |
1748 KB |
Output is correct |
28 |
Correct |
2 ms |
1876 KB |
Output is correct |
29 |
Correct |
21 ms |
1356 KB |
Output is correct |
30 |
Correct |
28 ms |
1440 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
3 ms |
2004 KB |
Output is correct |
33 |
Correct |
2 ms |
1364 KB |
Output is correct |
34 |
Correct |
22 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
696 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
576 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
564 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
704 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
724 KB |
Output is correct |
49 |
Correct |
1 ms |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
724 KB |
Output is correct |
51 |
Correct |
1 ms |
724 KB |
Output is correct |
52 |
Correct |
1 ms |
724 KB |
Output is correct |
53 |
Correct |
1 ms |
692 KB |
Output is correct |
54 |
Correct |
1 ms |
828 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
696 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
576 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
1492 KB |
Output is correct |
24 |
Correct |
2 ms |
1504 KB |
Output is correct |
25 |
Correct |
1 ms |
1748 KB |
Output is correct |
26 |
Correct |
2 ms |
2080 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
1108 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
952 KB |
Output is correct |
36 |
Correct |
1 ms |
980 KB |
Output is correct |
37 |
Correct |
1 ms |
852 KB |
Output is correct |
38 |
Correct |
1 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
308 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
596 KB |
Output is correct |
53 |
Correct |
1 ms |
596 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
564 KB |
Output is correct |
59 |
Correct |
1 ms |
596 KB |
Output is correct |
60 |
Correct |
5 ms |
1108 KB |
Output is correct |
61 |
Correct |
1 ms |
1464 KB |
Output is correct |
62 |
Correct |
33 ms |
1748 KB |
Output is correct |
63 |
Correct |
2 ms |
1876 KB |
Output is correct |
64 |
Correct |
21 ms |
1356 KB |
Output is correct |
65 |
Correct |
28 ms |
1440 KB |
Output is correct |
66 |
Correct |
4 ms |
1876 KB |
Output is correct |
67 |
Correct |
3 ms |
2004 KB |
Output is correct |
68 |
Correct |
2 ms |
1364 KB |
Output is correct |
69 |
Correct |
22 ms |
1484 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
704 KB |
Output is correct |
75 |
Correct |
1 ms |
596 KB |
Output is correct |
76 |
Correct |
1 ms |
724 KB |
Output is correct |
77 |
Correct |
1 ms |
596 KB |
Output is correct |
78 |
Correct |
1 ms |
724 KB |
Output is correct |
79 |
Correct |
1 ms |
724 KB |
Output is correct |
80 |
Correct |
1 ms |
724 KB |
Output is correct |
81 |
Correct |
1 ms |
724 KB |
Output is correct |
82 |
Correct |
1 ms |
724 KB |
Output is correct |
83 |
Correct |
1 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
692 KB |
Output is correct |
85 |
Correct |
1 ms |
828 KB |
Output is correct |
86 |
Correct |
1 ms |
596 KB |
Output is correct |
87 |
Correct |
4 ms |
1108 KB |
Output is correct |
88 |
Correct |
0 ms |
340 KB |
Output is correct |
89 |
Correct |
1 ms |
340 KB |
Output is correct |
90 |
Correct |
1 ms |
1108 KB |
Output is correct |
91 |
Correct |
1 ms |
852 KB |
Output is correct |
92 |
Correct |
27 ms |
3004 KB |
Output is correct |
93 |
Correct |
18 ms |
2196 KB |
Output is correct |
94 |
Correct |
2 ms |
1492 KB |
Output is correct |
95 |
Correct |
1 ms |
1108 KB |
Output is correct |
96 |
Correct |
1 ms |
1332 KB |
Output is correct |
97 |
Correct |
5 ms |
1620 KB |
Output is correct |
98 |
Correct |
1 ms |
1620 KB |
Output is correct |
99 |
Correct |
8 ms |
1848 KB |
Output is correct |
100 |
Correct |
1 ms |
1468 KB |
Output is correct |
101 |
Correct |
2 ms |
1364 KB |
Output is correct |
102 |
Correct |
29 ms |
2172 KB |
Output is correct |
103 |
Correct |
2 ms |
1876 KB |
Output is correct |
104 |
Correct |
7 ms |
1876 KB |
Output is correct |
105 |
Correct |
1 ms |
852 KB |
Output is correct |
106 |
Correct |
17 ms |
1812 KB |
Output is correct |
107 |
Correct |
16 ms |
2128 KB |
Output is correct |