제출 #366103

#제출 시각아이디문제언어결과실행 시간메모리
366103Jarif_RahmanBitaro’s Party (JOI18_bitaro)C++17
14 / 100
1790 ms415076 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int sq;
vector<bool> bl;
bool comp(pair<int, int> a, pair<int, int> b){return a > b;}
void merge(vector<pair<int, int>> a, vector<pair<int, int>> &b){
	b.insert(b.end(), a.begin(), a.end());
	int el = min(sq, (int)b.size());
	nth_element(b.begin(), b.begin()+el, b.end(), comp);
	b.resize(el);
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q; cin >> n >> m >> q;
    bl.resize(n, 0);
    sq = sqrt(n+0.0);
    vector<vector<int>> v(n);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
    }
    vector<vector<pair<int, int>>> dps(n);
    for(int i = 0; i < n; i++) dps[i].pb({0, i});
    for(int i = 0; i < n; i++){
        auto mg = dps[i];
        for(auto &[c, nd]: mg) c++;
        for(int x: v[i]) merge(mg, dps[x]);
    }
    vector<bool> busy(n, 0);
    while(q--){
        int tj, yj; cin >> tj >> yj;
        tj--;
        vector<int> cbs(yj);
        for(int &x: cbs) cin >> x, x--, busy[x] = 1;
        if(yj < sq){
            int ans = busy[tj] ? -1 : 0;
            for(auto [c, nd]: dps[tj]) if(!busy[nd]) ans = max(ans, c);
            cout << ans << "\n";
            for(int x: cbs) busy[x] = 0;
            continue;
        }
        vector<int> dp(n, -1);
        for(int i = 0; i < n; i++){
            if(busy[i] && dp[i] == -1) continue;
            dp[i] = max(dp[i], 0);
            for(int x: v[i]) dp[x] = max(dp[x], dp[i]+1);
        }
        cout << dp[tj] << "\n";
        for(int x: cbs) busy[x] = 0;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...