#include<bits/stdc++.h>
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxn=500;
const ll inf=1e18;
const ll mod=1e9+7;
struct TrieNode {
int nxt[2], link;
};
const ll maxN=2e5;
vector<TrieNode> Trie;
vector<ll> adj[maxN];
ll numNode, nTree, numQuery;
bool ed[maxn];
ll a[maxn];
void insertString(ll len) {
int pt(0);
for (int i=1;i<=len;i++) {
int c=a[i];
if(!Trie[pt].nxt[c]) {
Trie[pt].nxt[c] = Trie.size();
Trie.emplace_back();
}
pt = Trie[pt].nxt[c];
}
ed[pt]=true;
}
void buildAutomaton(void) {
queue<int> qu;
qu.push(0);
while(qu.size()) {
int v(qu.front()), u(Trie[v].link);
qu.pop();
for (int c = 0; c < 2; ++c) {
if(Trie[v].nxt[c]) {
Trie[Trie[v].nxt[c]].link = (v) ? Trie[u].nxt[c] : 0;
qu.push(Trie[v].nxt[c]);
} else {
Trie[v].nxt[c] = Trie[u].nxt[c];
}
}
}
}
ll z,m,n;
vector<pli>vec[maxn];
vector<ll>cc[maxn];
ll dp[300][55][55];
void solve()
{
Trie.emplace_back();
cin >> z >> n >> m;
ll ct=z;
for(int i=1;i<=n;i++)
{
ll x;
cin >> x;
ll k;
cin >> k;
vector<ll>bd;
while(k--)
{
ll z;
cin >> z;
bd.pb(z);
}
if(bd.size()>=2)
{
ll u=x;
for(int j=0;j+2<bd.size();j++)
{
ct++;
vec[u].pb({bd[j],ct});
u=ct;
}
vec[u].pb({bd[bd.size()-2],bd.back()});
}
else
{
cc[x].pb(bd[0]);
}
}
for(int i=0;i<=50;i++) ed[i]=false;
for(int i=1;i<=m;i++)
{
ll k;
cin >> k;
for(int j=1;j<=k;j++)
{
cin >> a[j];
}
insertString(k);
}
buildAutomaton();
numNode=Trie.size();
//cout << Trie.size()<<'\n';
for(int i=0;i<=ct;i++)
{
for(int j=0;j<numNode;j++)
{
for(int k=0;k<numNode;k++)
{
dp[i][j][k]=inf;
}
}
}
for(int i=0;i<2;i++)
{
for(int u=0;u<numNode;u++)
{
ll luu=u;
while(u!=0&&Trie[u].nxt[i]==0)
{
u=Trie[u].link;
}
//cout << Trie[u].nxt[i]<<'\n';
if(ed[Trie[u].nxt[i]]==0)
{
dp[i][luu][Trie[u].nxt[i]]=1;
//cout << i<<' '<<luu << '\n';
}
u=luu;
}
}
for(int turn=1;turn<=n;turn++)
{
for(int k=0;k<numNode;k++)
{
for(int i=0;i<=ct;i++)
{
for(int j=0;j<numNode;j++)
{
for(int q=0;q<numNode;q++)
{
for(auto zz:vec[i])
{
dp[i][j][q]=min(dp[i][j][q],dp[zz.fi][j][k]+dp[zz.se][k][q]);
}
for(auto zz:cc[i])
{
dp[i][j][q]=min(dp[i][j][q],dp[zz][j][q]);
}
}
}
}
}
}
for(int i=2;i<=z-1;i++)
{
ll ans=inf;
for(int j=0;j<numNode;j++)
{
ans=min(ans,dp[i][0][j]);
}
if(ans!=inf) cout <<"NO "<< ans << '\n';
else cout << "YES" << '\n';
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
Viruses.cpp: In function 'void solve()':
Viruses.cpp:75:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int j=0;j+2<bd.size();j++)
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10064 KB |
Output is correct |
2 |
Correct |
3 ms |
10064 KB |
Output is correct |
3 |
Correct |
2 ms |
8016 KB |
Output is correct |
4 |
Correct |
3 ms |
8016 KB |
Output is correct |
5 |
Correct |
3 ms |
8016 KB |
Output is correct |
6 |
Correct |
4 ms |
8016 KB |
Output is correct |
7 |
Correct |
3 ms |
8016 KB |
Output is correct |
8 |
Correct |
3 ms |
8016 KB |
Output is correct |
9 |
Correct |
3 ms |
8016 KB |
Output is correct |
10 |
Correct |
3 ms |
8016 KB |
Output is correct |
11 |
Correct |
3 ms |
8016 KB |
Output is correct |
12 |
Correct |
3 ms |
8016 KB |
Output is correct |
13 |
Correct |
2 ms |
8016 KB |
Output is correct |
14 |
Correct |
3 ms |
8016 KB |
Output is correct |
15 |
Correct |
2 ms |
8016 KB |
Output is correct |
16 |
Correct |
3 ms |
8016 KB |
Output is correct |
17 |
Correct |
2 ms |
8184 KB |
Output is correct |
18 |
Correct |
2 ms |
7872 KB |
Output is correct |
19 |
Incorrect |
2 ms |
8016 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10064 KB |
Output is correct |
2 |
Correct |
3 ms |
10064 KB |
Output is correct |
3 |
Correct |
690 ms |
8016 KB |
Output is correct |
4 |
Execution timed out |
1068 ms |
10064 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
690 ms |
8016 KB |
Output is correct |
2 |
Execution timed out |
1068 ms |
10064 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10064 KB |
Output is correct |
2 |
Correct |
3 ms |
10064 KB |
Output is correct |
3 |
Correct |
2 ms |
8016 KB |
Output is correct |
4 |
Correct |
3 ms |
8016 KB |
Output is correct |
5 |
Correct |
3 ms |
8016 KB |
Output is correct |
6 |
Correct |
4 ms |
8016 KB |
Output is correct |
7 |
Correct |
3 ms |
8016 KB |
Output is correct |
8 |
Correct |
3 ms |
8016 KB |
Output is correct |
9 |
Correct |
3 ms |
8016 KB |
Output is correct |
10 |
Correct |
3 ms |
8016 KB |
Output is correct |
11 |
Correct |
3 ms |
8016 KB |
Output is correct |
12 |
Correct |
3 ms |
8016 KB |
Output is correct |
13 |
Correct |
2 ms |
8016 KB |
Output is correct |
14 |
Correct |
3 ms |
8016 KB |
Output is correct |
15 |
Correct |
2 ms |
8016 KB |
Output is correct |
16 |
Correct |
3 ms |
8016 KB |
Output is correct |
17 |
Correct |
2 ms |
8184 KB |
Output is correct |
18 |
Correct |
2 ms |
7872 KB |
Output is correct |
19 |
Incorrect |
2 ms |
8016 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10064 KB |
Output is correct |
2 |
Correct |
3 ms |
10064 KB |
Output is correct |
3 |
Correct |
2 ms |
8016 KB |
Output is correct |
4 |
Correct |
3 ms |
8016 KB |
Output is correct |
5 |
Correct |
3 ms |
8016 KB |
Output is correct |
6 |
Correct |
4 ms |
8016 KB |
Output is correct |
7 |
Correct |
3 ms |
8016 KB |
Output is correct |
8 |
Correct |
3 ms |
8016 KB |
Output is correct |
9 |
Correct |
3 ms |
8016 KB |
Output is correct |
10 |
Correct |
3 ms |
8016 KB |
Output is correct |
11 |
Correct |
3 ms |
8016 KB |
Output is correct |
12 |
Correct |
3 ms |
8016 KB |
Output is correct |
13 |
Correct |
2 ms |
8016 KB |
Output is correct |
14 |
Correct |
3 ms |
8016 KB |
Output is correct |
15 |
Correct |
2 ms |
8016 KB |
Output is correct |
16 |
Correct |
3 ms |
8016 KB |
Output is correct |
17 |
Correct |
2 ms |
8184 KB |
Output is correct |
18 |
Correct |
2 ms |
7872 KB |
Output is correct |
19 |
Incorrect |
2 ms |
8016 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |