제출 #1262534

#제출 시각아이디문제언어결과실행 시간메모리
1262534nathlol2Bitaro’s Party (JOI18_bitaro)C++20
0 / 100
7 ms8264 KiB
#include <bits/stdc++.h>
#define fi first
#define sc second
using namespace std;
const int N = 100005;
const int SQ = 330;
vector<vector<int>> g(N), rv(N);
vector<pair<int, int>> dp[N];
int ans = -1;
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 0;i<m;i++){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        rv[v].push_back(u);
    }
    dp[1].push_back({0, 1});
    for(int i = 2;i<=n;i++){
        vector<int> ptr(rv[i].size(), 0);
        unordered_set<int> s;
        for(int c = 0;c<SQ;c++){
            pair<int, int> a = {-1, -1};
            int idx = -1;
            for(int j = 0;j<rv[i].size();j++){
                if(ptr[j] < dp[rv[i][j]].size()){
                    while(ptr[j] < dp[rv[i][j]].size() && s.count(dp[rv[i][j]][ptr[j]].sc)){
                        ++ptr[j];
                    }
                    if(ptr[j] < dp[rv[i][j]].size() && dp[rv[i][j]][ptr[j]].fi > a.fi){
                        a.fi = dp[rv[i][j]][ptr[j]].fi;
                        a.sc = dp[rv[i][j]][ptr[j]].sc;
                        idx = j;
                    }
                }
            }
            if(idx == -1){
                break;
            }
            ++ptr[idx];
            dp[i].push_back({a.fi + 1, a.sc});
            s.insert(a.sc);
        }
        dp[i].push_back({0, i});
    }
    bool come[N];
    for(int i = 1;i<N;i++){
        come[i] = 1;
    }
    while(q--){
        int x, h;
        cin >> x >> h;
        int a[h];
        for(int i = 0;i<h;i++){
            cin >> a[i];
            come[a[i]] = 0;
        }
        if(h < SQ){
            for(auto p : dp[x]){
                if(come[p.sc]){
                    ans = p.fi;
                    break;
                }
            }
        }else{
            vector<int> mx(N, -1);
            mx[x] = 0;
            for(int i = x - 1;i>=1;i--){
                for(auto u : g[i]){
                    if(u > x) continue;
                    mx[i] = max(mx[i], mx[u]);
                }
            }
            for(int i = 1;i<=x;i++){
                if(come[i]){
                    ans = max(ans, mx[i]);
                }
            }
        }
        for(int i = 0;i<h;i++){
            come[a[i]] = 1;
        }
        cout << ans << '\n';
        ans = -1;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...