Submission #920176

#TimeUsernameProblemLanguageResultExecution timeMemory
920176Alihan_8Bitaro’s Party (JOI18_bitaro)C++17
7 / 100
2041 ms9436 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int B = 321;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, q; cin >> n >> m >> q;
    vector <vector<int>> G(n);
    for ( int i = 1; i <= m; i++ ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[u].pb(v);
    }
    vector <vector<int>> dp(n, vector <int> (B, -1)), fa(n, vector <int> (B, -1));
    for ( int i = 0; i < n; i++ ){
        dp[i][0] = 0;
        fa[i][0] = i;
    }
    for ( int u = 0; u < n; u++ ){
        auto prev = [&](auto &q){
            auto f = q.top();
            q.pop();
            auto ret = f;
            if ( q.empty() ){
                ret = {-1, -1};
            } else ret = q.top();
            q.push(f);
            return ret;
        };
        for ( auto &v: G[u] ){
            priority_queue <pair<int,int>> q;
            for ( int j = 0; j < B; j++ ){
                if ( dp[u][j] != -1 ){
                    q.push({dp[u][j] + 1, fa[u][j]});
                }
            }
            for ( int j = 0; j < B; j++ ){
                int L = dp[v][j], R = fa[v][j];
                while ( !q.empty() && q.top().first == -1 ){
                    q.pop();
                }
                if ( q.empty() ) break;
                if ( fa[v][j] == q.top().second ){
                    auto [d, p] = prev(q);
                    if ( chmax(dp[v][j], d) ){
                        fa[v][j] = p;
                        auto f = q.top();
                        q.pop(), q.pop();
                        q.push(f);
                        q.push({L, R});
                    }
                } else if ( chmax(dp[v][j], q.top().first) ){
                    fa[v][j] = q.top().second;
                    q.pop();
                    q.push({L, R});
                }
            }
        }
    }
    vector <int> us(n);
    while ( q-- ){
        int t, y; cin >> t >> y;
        --t;
        vector <int> c(y);
        for ( auto &x: c ){
            cin >> x; --x;
            us[x] = true;
        }
        if ( y + 1 < B ){
            int opt = -1;
            for ( int j = 0; j < B; j++ ){
                if ( dp[t][j] != -1 && !us[fa[t][j]] ){
                    opt = dp[t][j];
                    break;
                }
            }
            cout << opt << ln;
        } else{
            vector <int> dp(n, -1);
            for ( int i = 0; i < n; i++ ){
                if ( !us[i] ) chmax(dp[i], 0LL);
                for ( auto &v: G[i] ){
                    if ( dp[i] != -1 ){
                        chmax(dp[v], dp[i] + 1);
                    }
                }
            }
            cout << dp[t] << ln;
        }
        for ( auto &x: c ){
            us[x] = false;
        }
    }

    cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...