Submission #95336

#TimeUsernameProblemLanguageResultExecution timeMemory
95336dalgerokBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1837 ms243936 KiB
#include<bits/stdc++.h>
using namespace std;




const int N = 1e5 + 5, S = 200;


int n, m, q;
vector < pair < int, int > > dp[N];
vector < int > g[N];
bool used[N];
int dpp[N], ans[N];


inline void mrg(int x, int y){
    for(auto it : dp[x]){
        ans[it.second] = it.first;
    }
    for(auto it : dp[y]){
        ans[it.second] = max(ans[it.second], it.first + 1);
    }
    for(auto &it : dp[x]){
        it.first = ans[it.second];
        ans[it.second] = 0;
    }
    for(auto it : dp[y]){
        if(ans[it.second]){
            dp[x].push_back(make_pair(it.first + 1, it.second));
            ans[it.second] = 0;
        }
    }
    sort(dp[x].rbegin(), dp[x].rend());
    while((int)dp[x].size() > S){
        dp[x].pop_back();
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m >> q;
    for(int i = 1; i <= m; i++){
        int x, y;
        cin >> x >> y;
        g[y].push_back(x);
    }
    for(int i = 1; i <= n; i++){
        for(int j : g[i]){
            mrg(i, j);
        }
        if((int)dp[i].size() < S){
            dp[i].push_back(make_pair(0, i));
        }
    }
    while(q--){
        int v, x;
        cin >> v >> x;
        vector < int > y(x);
        for(int i = 0; i < x; i++){
            cin >> y[i];
            used[y[i]] = true;
        }
        if(x < S){
            for(auto it : dp[v]){
                if(!used[it.second]){
                    cout << it.first << "\n";
                    goto to;
                }
            }
            cout << "-1\n";
            to:;
        }
        else{
            for(int i = 1; i <= v; i++){
                dpp[i] = 0;
                if(used[i]){
                    dpp[i] = -1;
                }
                for(int j : g[i]){
                    if(dpp[j] != -1){
                        dpp[i] = max(dpp[i], dpp[j] + 1);
                    }
                }
            }
            cout << dpp[v] << "\n";
        }
        for(int i = 0; i < x; i++){
            used[y[i]] = false;
        }
        y.clear();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...