This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e5+5;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<30);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll n,m,q,x,y,ans=-1,T,is[N],dp[N],id[N];
vector<ll> lis,ed[N],red[N];
vector<pll> pre[N];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m>>q;
REP(i,m){
cin>>x>>y;
ed[x].pb(y);
red[y].pb(x);
}
REP1(i,n){
for(ll j:red[i])id[j]=0;
REP(j,K){
pll mx=mp(i,0);
for(ll ii:red[i]){
while(id[ii]<SZ(pre[ii])&&is[pre[ii][id[ii]].X])++id[ii];
if(id[ii]>=SZ(pre[ii]))continue;
if(mx.Y<pre[ii][id[ii]].Y+1)mx=mp(pre[ii][id[ii]].X,pre[ii][id[ii]].Y+1);
}
//if(mx.X==i)break;
pre[i].pb(mx);
is[mx.X]=1;
lis.pb(mx.X);
}
for(ll j:lis)is[j]=0;
lis.clear();
}
while(q--){
cin>>x>>T;
REP(i,T)cin>>y,lis.pb(y),is[y]=1;
if(T>=K){
REP1(i,n)dp[i]=-INF;
dp[x]=0;
if(!is[x])ans=0;
for(int i=x-1;i>=1;i--){
for(auto j:ed[i]){
dp[i]=max(dp[i],dp[j]+1);
if(!is[i])ans=max(ans,dp[i]);
}
}
cout<<ans<<"\n";
}else{
for(auto i:pre[x])if(!is[i.X])ans=max(ans,i.Y);
cout<<ans<<"\n";
}
for(ll i:lis)is[i]=0;
ans=-1;
lis.clear();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |