Submission #366116

#TimeUsernameProblemLanguageResultExecution timeMemory
366116Jarif_RahmanBitaro’s Party (JOI18_bitaro)C++17
100 / 100
1913 ms420340 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<int> w;
bool comp(pair<int, int> a, pair<int, int> b){return a > b;}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q; cin >> n >> m >> q;
    sq = sqrt(n+0.0);
    w.resize(n, -1);
    vector<vector<int>> v(n), vv(n);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[b].pb(a);
        vv[a].pb(b);
    }
    vector<vector<pair<int, int>>> dps(n);
    for(int i = 0; i < n; i++){
        w[i] = 0;
        dps[i].pb({-1, i});
        for(int x: v[i]){
            for(auto [c, nd]: dps[x]){
                if(w[nd] == -1) w[nd] = c+1, dps[i].pb({-1, nd});
                else w[nd] = max(w[nd], c+1);
            }
        }
        int el = min(sq, (int)dps[i].size());
        for(auto &[c, nd]: dps[i]) c = w[nd], w[nd] = -1;
        nth_element(dps[i].begin(), dps[i].begin()+el, dps[i].end(), comp);
        while(dps[i].size() > sq) dps[i].pop_back();
    }
    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 <= tj; i++){
            if(busy[i] && dp[i] == -1) continue;
            dp[i] = max(dp[i], 0);
            for(int x: vv[i]) dp[x] = max(dp[x], dp[i]+1);
        }
        cout << dp[tj] << "\n";
        for(int x: cbs) busy[x] = 0;
    }
}

Compilation message (stderr)

bitaro.cpp: In function 'int main()':
bitaro.cpp:36:29: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |         while(dps[i].size() > sq) dps[i].pop_back();
      |               ~~~~~~~~~~~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...