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>
#define endl '\n'
using namespace std;
vector <int> v[200006];
int i,j,p,q,n,m,k,y,t;
int Q;
vector <pair <int,int>> best[200006];
const int crit = 150;
int used[200006];
int dp[200006];
int val[200006];
int marked[200006];
bool cmp(int x,int y)
{
return val[x]>val[y];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>Q;
for(i=1; i<=m; i++)
{
cin>>p>>q;
v[q].push_back(p);
}
for(i=1; i<=n; i++)
{
vector <int> all;
for(auto j:v[i])
{
if(marked[j]!=i)
{
marked[j]=i;
val[j]=1;
all.push_back(j);
}
else val[j]=max(val[j],1);
for(auto k:best[j])
{
if(marked[k.second]!=i)
{
marked[k.second]=i;
all.push_back(k.second);
val[k.second]=k.first+1;
}
else val[k.second]=max(val[k.second],k.first+1);
}
}
sort(all.begin(),all.end(),cmp);
for(int j=0;j<min((int)all.size(),crit);j++)
best[i].push_back({val[all[j]],all[j]});
}
while(Q--)
{
int party;
cin>>party;
cin>>y;
for(i=1; i<=y; i++)
{
cin>>t;
used[t]=Q+1;
}
bool fl=0;
if(!best[party].empty())
{
for(i=0; i<(int)best[party].size(); i++)
{
//cout<<best[party][i].first<<" "<<best[party][i].second<<endl;
if(used[best[party][i].second]!=Q+1)
{
// cout<<best[party][i].second<<" "<<best[party][i].first<<endl;
cout<<best[party][i].first<<endl;
fl=1;
break;
}
}
}
if(fl)continue;
if(y<crit)
{
if(used[party]==Q+1)
cout<<-1<<endl;
else cout<<0<<endl;
continue;
}
///bruteforce
fill(dp,dp+n+1,-1);
for(i=1;i<=party;i++)
{
if(used[i]==Q+1)
dp[i]=-1;
else dp[i]=0;
for(auto j:v[i])
{
if(dp[j]!=-1)
dp[i]=max(dp[i],dp[j]+1);
}
}
cout<<dp[party]<<endl;
}
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... |