Submission #768566

#TimeUsernameProblemLanguageResultExecution timeMemory
768566raysh07Bitaro’s Party (JOI18_bitaro)C++17
7 / 100
2096 ms477164 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 69, B = 300;
int n, m, q;
vector <int> adj[N];
pair <int, int> dp[N][B]; // {dist, who}
int ds[N];

void Solve() 
{
    cin >> n >> m >> q;
    
    for (int i = 1; i <= m; i++){
        int u, v; cin >> u >> v;
        
        adj[v].push_back(u);
    }
    
    for (int i = 0; i < N; i++){
        for (int j = 0; j < B; j++){
            dp[i][j] = {-1, -1};
        }
    }
    
    for (int i = 1; i <= n; i++){
        vector <pair<int, int>> ok;
        ok.push_back({0, i});
        for (auto j :adj[i]){
            for (int i1 = 0; i1 < B; i1++){
                if (dp[j][i1].first == -1) break;
                
                ok.push_back({dp[j][i1].first + 1, dp[j][i1].second});
            }
        }
        
        sort(ok.begin(), ok.end(), greater<pair<int, int>>());
        set <int> bruh;
        vector <pair<int, int>> nok;
        for (auto [x, y] : ok){
            if (bruh.find(y) == bruh.end()){
                nok.push_back({x, y});
                bruh.insert(y);
            }
        }
        ok = nok;
        for (int j = 0; j < min(B, (int)ok.size()); j++){
            dp[i][j] = ok[j];
        }
    }
    
    while (q--){
        int x, k; cin >> x >> k;
        int kk = k;
        set <int> st;
        while (k--){
            int y; cin >> y;
            st.insert(y);
        }
        
        if (kk < B){
            for (int i = 0; i < B; i++){
                if (st.find(dp[x][i].second) == st.end()){
                    cout << dp[x][i].first << "\n";
                    break;
                }
            }
            
            continue;
        }
        
        for (int i = 1; i <= n; i++) ds[i] = -INF;
        ds[x] = 0;
        
        for (int i = x; i >= 1; i--){
            for (auto j : adj[i]){
                ds[j] = max(ds[j], ds[i] + 1);
            }
        }
        
        // for (int i = 1; i <= n; i++){
        //     cout << ds[i] << " ";
        // }
        // cout << "\n";
        
        int mx = -1;
        for (int i = 1; i <= n; i++) if (ds[i] != -INF && st.find(i) == st.end()) mx = max(mx, ds[i]);
        
        cout << mx << "\n";
    }
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...