Submission #447959

#TimeUsernameProblemLanguageResultExecution timeMemory
447959zaneyuBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1895 ms332872 KiB
/*input
12 17 10
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 6
6 7
7 8
5 9
6 10
7 11
8 12
9 10
10 11
11 12
6 3 1 7 12
3 7 1 2 3 4 5 6 7
11 3 1 3 5
9 2 1 9
8 4 1 2 3 4
1 1 1
12 0
10 3 1 6 10
11 8 2 3 5 6 7 9 10 11
8 7 2 3 4 5 6 7 8
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
const int maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
const int sq=400;
vector<int> v[maxn],rev[maxn];
int dp[maxn];
pii mx[maxn][sq+5];
bool in[maxn];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,m,q;
    cin>>n>>m>>q;
    REP(i,m){
        int a,b;
        cin>>a>>b;
        --a,--b;
        v[a].pb(b);
        rev[b].pb(a);
    }
    REP(i,n) REP(j,sq) mx[i][j]={0,n};
    REP(i,n){
        REP(j,sq){
            //cout<<i<<' '<<mx[i][j].f<<' '<<mx[i][j].s<<'\n';
            if(mx[i][j].s==n){
                mx[i][j]={0,i};
                break;
            }
        }   
        for(auto x:v[i]){
            int l=0,r=0;
            vector<pii> ans;
            while(sz(ans)<sq and (l<sq or r<sq)){
                pii tmp={mx[i][r].f+1,mx[i][r].s};
                if(mx[i][r].s==n and mx[x][l].s==n) break;
                if(mx[i][r].s==n) tmp={-1,-1};
                if(r==sq or mx[x][l]>tmp){
                    if(!in[mx[x][l].s]) ans.pb(mx[x][l]);
                    ++l;
                }
                else{
                    if(!in[mx[i][r].s]) ans.pb(tmp);
                    ++r;
                }
                in[ans.back().s]=1;
            }
            REP(j,sz(ans)) mx[x][j]=ans[j],in[ans[j].s]=0;
        }
    }
    in[n]=1;
    while(q--){
        int x,y;
        cin>>x>>y;
        --x;
        if(y<sq){
            vector<int> v;
            REP(i,y){
                int z;
                cin>>z;
                --z;
                v.pb(z),in[z]=1;
            }
            int ans=-1;
            for(auto z:mx[x]){
                if(!in[z.s]) MXTO(ans,z.f);
            }
            for(auto x:v) in[x]=0;
            cout<<ans<<'\n';
        }
        else{
            REP(i,n) dp[i]=-INF;
            REP(i,y){
                int z;
                cin>>z;
                --z;
                in[z]=1;
            }
            dp[x]=0;
            for(int i=x;i>=0;i--){
                for(auto z:rev[i]){
                    MXTO(dp[z],dp[i]+1);
                }
            }
            int ans=-1;
            REP(i,x+1) if(!in[i]) MXTO(ans,dp[i]);
            REP(i,n) in[i]=0;
            cout<<ans<<'\n';
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...