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>
#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 val[maxn];
ll numNode, nTree, numQuery;
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];
}
val[pt]=1;
}
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];
}
}
}
}
void preDfs(int u,int p) {
for (int it = 0; it < int(adj[u].size()); ++it) {
int v(adj[u][it]);
val[v]+=val[u];
preDfs(v,u);
}
}
void buildTree(void) {
nTree = Trie.size();
for (int i = 1; i < nTree; ++i)
adj[Trie[i].link].push_back(i);
preDfs(0,0);
}
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=1;i<=m;i++)
{
ll k;
cin >> k;
for(int j=1;j<=k;j++)
{
cin >> a[j];
}
insertString(k);
}
buildAutomaton();
numNode=Trie.size();
buildTree();
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(val[Trie[u].nxt[i]]==0)
{
dis[i][luu][Trie[u].nxt[i]]=1;
}
u=luu;
}
}
//cout << nxt[1][0]<<'\n';
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]);
}
}
}
}
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();
}
# | 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... |