Submission #963263

#TimeUsernameProblemLanguageResultExecution timeMemory
963263TimAniBitaro’s Party (JOI18_bitaro)C++17
7 / 100
2045 ms293072 KiB
//start-time: 2024-04-14 19:39:32
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll w = round(sqrt(100000)) + 1;

void solve(){
    ll n, m, q;
    cin >> n >> m >> q;
    vector<vector<ll>> g(n);
    vector<vector<ll>> gR(n);
    for(ll i = 0; i < m; i++){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        gR[v].push_back(u);
    }
    
    vector<ll> vis(n), topo;
    auto dfs = [&](ll u, auto&&dfs) -> void {
        vis[u] = 1;
        for(auto v : g[u]){
            if(!vis[v]){
                dfs(v, dfs);
            }
        }
        vis[u] = 2;
        topo.push_back(u);
    };

    for(int i = 0; i < n; i++){
        if(vis[i] == 0){
            dfs(i, dfs);
        }
    }

    reverse(topo.begin(), topo.end());
    
    vector<vector<array<ll, 2>>> dp(n);

    for(auto u : topo){
        vector<ll> val(n, -1), aux;
        aux.push_back(u);
        val[u] = 0;

        for(auto v : gR[u]){
            for(auto [value, ind] : dp[v]){
                aux.push_back(ind);
                val[ind] = max(val[ind], value + 1);
            }
        }

        sort(aux.begin(), aux.end());
        aux.erase(unique(aux.begin(), aux.end()), aux.end());
        sort(aux.begin(), aux.end(), [&](int a, int b){
            return val[a] > val[b];
        });
        for(auto el : aux){
            if(dp[u].size() < w){
                dp[u].push_back({val[el], el});
            }
        }
    }

    for(ll i = 0; i < q; i++){
        ll u, y;
        cin >> u >> y;
        u--;
        vector<bool> c(100'001);
        for(ll j = 0; j < y; j++){
            ll a; cin >> a;
            c[--a] = 1;
        }

        ll ans = -1;
        if(y < w) {
            for(auto [v, i] : dp[u]){
                if(!c[i]){
                    ans = max(ans, v);
                }
            }
        }
        else {
            vector<int> naive(n);
            for(auto v : topo){
                naive[v] = c[v] ? -1 : 0;
                for(auto el : gR[v]){
                    naive[v] = max(naive[v], naive[el] + (naive[el] != -1));
                }
            }
            ans = naive[u];
        }
        
            //for(auto el : naive){
                //cout << el << ' ';
            //}
            //cout << endl;
        cout << ans << endl;
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll T = 1;
    //cin >> T;
    while(T--) solve();
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...