#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[2][55][55];
vector<vector<ll>>query;
vector<ll>id;
ll nxt[55][55],dis[100][55][55];
void solve()
{
Trie.emplace_back();
cin >> z >> n >> m;
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);
}
query.pb(bd);
id.pb(x);
}
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<=z;i++)
{
for(int j=0;j<numNode;j++)
{
for(int k=0;k<numNode;k++)
{
dis[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;
}
nxt[luu][i]=Trie[u].nxt[i];
if(ed[Trie[u].nxt[i]]==0)
{
dis[i][luu][Trie[u].nxt[i]]=1;
}
u=luu;
}
}
for(int turn=0;turn<n;turn++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<numNode;j++)
{
for(int q=0;q<numNode;q++)
{
dp[0][j][q]=dp[1][j][q]=inf;
}
}
int p=0;
for(int j=0;j<numNode;j++) dp[p][j][j]=0;
for(auto x:query[i])
{
for(int j=0;j<numNode;j++)
{
for(int q=0;q<numNode;q++)
{
if(dp[p][j][q]==inf) continue;
if(x<=1)
{
dp[p^1][j][nxt[q][x]]=min(dp[p^1][j][nxt[q][x]],dp[p][j][q]+dis[x][q][nxt[q][x]]);
}
for(int k=0;k<numNode;k++)
{
dp[p^1][j][k]=min(dp[p^1][j][k],dp[p][j][q]+dis[x][q][k]);
}
dp[p][j][q]=inf;
}
}
p^=1;
}
for(int j=0;j<numNode;j++)
{
for(int q=0;q<numNode;q++)
{
dis[id[i]][j][q]=min(dis[id[i]][j][q],dp[p][j][q]);
}
}
}
//cout << dis[2][3][1]<<'\n';
}
for(int i=2;i<=z-1;i++)
{
ll ans=inf;
for(int j=0;j<numNode;j++)
{
ans=min(ans,dis[i][0][j]);
}
if(ans!=inf) cout <<"NO "<< ans << '\n';
else cout << "YES" << '\n';
}
}
int main()
{
fastio
//freopen("c.INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7672 KB |
Output is correct |
2 |
Correct |
98 ms |
7504 KB |
Output is correct |
3 |
Correct |
33 ms |
7504 KB |
Output is correct |
4 |
Correct |
31 ms |
7636 KB |
Output is correct |
5 |
Correct |
18 ms |
7504 KB |
Output is correct |
6 |
Correct |
10 ms |
7504 KB |
Output is correct |
7 |
Correct |
8 ms |
7632 KB |
Output is correct |
8 |
Correct |
2 ms |
7248 KB |
Output is correct |
9 |
Correct |
2 ms |
7504 KB |
Output is correct |
10 |
Correct |
3 ms |
7248 KB |
Output is correct |
11 |
Correct |
4 ms |
7248 KB |
Output is correct |
12 |
Correct |
2 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7416 KB |
Output is correct |
14 |
Correct |
3 ms |
7248 KB |
Output is correct |
15 |
Correct |
2 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
3 ms |
7504 KB |
Output is correct |
18 |
Correct |
3 ms |
7248 KB |
Output is correct |
19 |
Correct |
2 ms |
7248 KB |
Output is correct |
20 |
Correct |
2 ms |
7248 KB |
Output is correct |
21 |
Correct |
2 ms |
7248 KB |
Output is correct |
22 |
Correct |
2 ms |
7248 KB |
Output is correct |
23 |
Correct |
2 ms |
7248 KB |
Output is correct |
24 |
Correct |
3 ms |
7504 KB |
Output is correct |
25 |
Correct |
12 ms |
5628 KB |
Output is correct |
26 |
Correct |
89 ms |
7624 KB |
Output is correct |
27 |
Correct |
234 ms |
7504 KB |
Output is correct |
28 |
Correct |
19 ms |
7504 KB |
Output is correct |
29 |
Correct |
122 ms |
7504 KB |
Output is correct |
30 |
Correct |
145 ms |
7504 KB |
Output is correct |
31 |
Correct |
20 ms |
7632 KB |
Output is correct |
32 |
Correct |
17 ms |
7628 KB |
Output is correct |
33 |
Correct |
34 ms |
7504 KB |
Output is correct |
34 |
Correct |
68 ms |
7504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |