Submission #1105772

# Submission time Handle Problem Language Result Execution time Memory
1105772 2024-10-27T17:26:42 Z mrwang Viruses (BOI20_viruses) C++14
25 / 100
221 ms 7672 KB
#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
1 Correct 3 ms 7248 KB Output is correct
2 Incorrect 2 ms 7248 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Incorrect 2 ms 7248 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 7504 KB Output is correct
2 Correct 87 ms 7504 KB Output is correct
3 Correct 31 ms 7504 KB Output is correct
4 Correct 33 ms 7504 KB Output is correct
5 Correct 16 ms 7672 KB Output is correct
6 Correct 9 ms 7504 KB Output is correct
7 Correct 7 ms 7504 KB Output is correct
8 Correct 2 ms 7248 KB Output is correct
9 Correct 2 ms 7248 KB Output is correct
10 Correct 3 ms 7504 KB Output is correct
11 Correct 2 ms 7248 KB Output is correct
12 Correct 2 ms 7248 KB Output is correct
13 Correct 2 ms 7504 KB Output is correct
14 Correct 3 ms 7248 KB Output is correct
15 Correct 3 ms 7248 KB Output is correct
16 Correct 3 ms 7248 KB Output is correct
17 Correct 2 ms 7348 KB Output is correct
18 Correct 2 ms 7248 KB Output is correct
19 Correct 3 ms 7248 KB Output is correct
20 Correct 3 ms 7248 KB Output is correct
21 Correct 2 ms 7248 KB Output is correct
22 Correct 3 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 11 ms 5624 KB Output is correct
26 Correct 94 ms 7504 KB Output is correct
27 Correct 221 ms 7504 KB Output is correct
28 Correct 18 ms 7504 KB Output is correct
29 Correct 114 ms 7616 KB Output is correct
30 Correct 160 ms 7504 KB Output is correct
31 Correct 18 ms 7504 KB Output is correct
32 Correct 16 ms 7504 KB Output is correct
33 Correct 30 ms 7504 KB Output is correct
34 Correct 62 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Incorrect 2 ms 7248 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7248 KB Output is correct
2 Incorrect 2 ms 7248 KB Output isn't correct
3 Halted 0 ms 0 KB -