Submission #1158528

#TimeUsernameProblemLanguageResultExecution timeMemory
1158528akzytrBitaro’s Party (JOI18_bitaro)C++20
100 / 100
797 ms144260 KiB
#include <bits/stdc++.h>
#define ve vector
#define ar array 
#define pb push_back
#define ins insert

#define endl '\n'
#define ll long long
using namespace std;

const int MXN = 1e5+1;
ve<int> adj[MXN];
int blck[MXN];
int from[MXN];
ll dp[MXN];

ve<pair<int, int>> madst[MXN+1];
const int BLCK = 100;

ll ansgy(ll x, ll c){
    fill(dp, dp+MXN, -1);
    dp[x] = 0;

    ll ans = -1;
    for(int i = x; i >= 1; i--){
        if(dp[i] == -1){continue;}
        for(int j : adj[i]){
            dp[j] = max(dp[j], dp[i]+1);
        }
        if(blck[i] != c){
            ans = max(ans, dp[i]);
        }
    }
    return ans;
}

void populate(ll x){
    madst[x].pb({0, x});
    ve<int> co;
    for(int i : adj[x]){
        for(auto [dst, y] : madst[i]){
            if(from[y] == -1){
                co.pb(y);
                from[y] = dst+1;
            }
            else{
                from[y] = max(from[y], dst+1);
            }
        }
    }
    for(int i : co){
        madst[x].pb({from[i], i});
        from[i] = -1;
    }
    sort(madst[x].rbegin(), madst[x].rend());
    int le = madst[x].size();
    while(le > BLCK){
        madst[x].pop_back();
        le--;
    }
}

int main(){
    fill(blck, blck+MXN, -1);
    fill(from, from+MXN, -1);

    int n, m, q;
    scanf("%d %d %d", &n, &m, &q);

    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[b].pb(a);
    }
    for(int i = 1; i <= n; i++){
        populate(i);
    }
    for(int i = 0; i < q; i++){
        int t, sz;
        scanf("%d %d", &t, &sz);
        int ans = -1;
        for(int j = 0; j < sz; j++){
            int y;
            scanf("%d", &y);
            blck[y] = i;
        }
        if(sz >= BLCK){
            ans = ansgy(t, i);
        }
        else{
            for(auto [u,v]: madst[t]){
                if(blck[v] != i){
                    ans = max(ans, u);
                }
            }
        }
        printf("%d\n",ans);
    }
}

Compilation message (stderr)

bitaro.cpp: In function 'int main()':
bitaro.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         scanf("%d %d", &t, &sz);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
bitaro.cpp:84:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |             scanf("%d", &y);
      |             ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...