Submission #1110762

# Submission time Handle Problem Language Result Execution time Memory
1110762 2024-11-10T11:41:29 Z Zero_OP Viruses (BOI20_viruses) C++14
11 / 100
1 ms 588 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }
        return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            return a = b, true;
        }
        return false;
    }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;

template<int ALPHABET = 26>
struct AhoCorasick{
    static const int C = ALPHABET;
    struct Node{
        int link, next[C]; bool v;
        Node(){
            link = -1;
            v = false;
            memset(next, -1, sizeof(next));
        }
    };

    vector<Node> Nodes;
    AhoCorasick() : Nodes(){
        Nodes.emplace_back();
    }

    int size(){ return sz(Nodes); }
    int go(int u, int c){ return Nodes[u].next[c]; }
    int link(int u){ return Nodes[u].link; }

    void addString(const vector<int>& s){
        int u = 0;
        for(auto c : s){
            if(Nodes[u].next[c] == -1){
                Nodes[u].next[c] = size();
                Nodes.emplace_back();
            }
            u = Nodes[u].next[c];
        }
        Nodes[u].v = true;
    }

    void build_links(){
        queue<int> q;
        q.push(0);
        while(!q.empty()){
            int u = q.front(), link = Nodes[u].link; q.pop();
            if(u > 0) Nodes[u].v |= Nodes[link].v;
            for(int c = 0; c < C; ++c){
                int& nxt = Nodes[u].next[c], nxt_link = (u ? Nodes[link].next[c] : 0);
                if(nxt == -1) nxt = nxt_link;
                else{
                    Nodes[nxt].next[c] = nxt_link;
                    q.push(nxt);
                }
            }
        }
    }

    bool check(int u){ return !Nodes[u].v; }
};

const ull inf = (1ULL << 63);

ull add(ull a, ull b){
    if(a == inf || b == inf) return inf;
    return a + b;
}

void upd(ull& a, ull b){
    if(a == inf || b == inf) a = inf;
    else a += b;
}

using item = pair<ull, int>; //for dijkstra

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int G, N, M;
    cin >> G >> N >> M;

    vector<int> from(N);
    vector<vector<int>> mutations(N);
    vector<vector<int>> options(G);

    for(int i = 0; i < N; ++i){
        int a, k;
        cin >> a >> k;
        from[i] = a;
        for(int j = 0; j < k; ++j){
            int b; cin >> b;
            mutations[i].emplace_back(b);
            options[b].emplace_back(i);
        }
    }

    AhoCorasick<2> T;
    for(int i = 0; i < M; ++i){
        int l; cin >> l;
        vector<int> s(l);
        for(int& x : s) cin >> x;
        T.addString(s);
    }

    if(M == 0){
        vector<ull> dp(G, inf);
        dp[0] = dp[1] = 1;

        priority_queue<item, vector<item>, greater<item>> pq;
        pq.push({1, 0}); pq.push({1, 1});

        vector<bool> vis(G, false);
        while(!pq.empty()){
            ull cost; int u;
            tie(cost, u) = pq.top(); pq.pop();

            if(vis[u]) continue;
            vis[u] = true;

            for(auto id_mutation : options[u]){
                int a = from[id_mutation];
                ull res = 0;
                for(auto x : mutations[id_mutation]){
                    upd(res, dp[x]);
                }

                if(minimize(dp[a], res)){
                    pq.push({dp[a], a});
                }
            }
        }

        for(int i = 2; i < G; ++i){
            if(dp[i] == inf) cout << "YES\n";
            else cout << "NO " << dp[i] << '\n';
        }
    }

    return 0;
}

/*

Testcase
subtask 1 :
5 6 0
2 2 1 0
2 2 3 2
3 1 4
4 2 3 2
4 2 0 0
3 2 1 1

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Incorrect 1 ms 336 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Incorrect 1 ms 336 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Incorrect 1 ms 336 KB Output isn't correct
22 Halted 0 ms 0 KB -