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>
using namespace std;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
vi al[100001];
vector<ii> lis[100001];
int vis[100001],vis1[100001],ans[100001];
int r=50,co=0;
vector<ii> freq[100001];
int vis2[100001];
void dfs(int node)
{
vis1[node]=1;
lli ma=0,mi=100000001;
for(int i=0;i<al[node].size();++i)
{
if(!vis1[al[node][i]])
dfs(al[node][i]);
freq[1+lis[al[node][i]][0].F].pb(mp(al[node][i],0));
ma=max(ma,1+lis[al[node][i]][0].F);
mi=min(mi,1+lis[al[node][i]][0].F);
}
while(lis[node].size()<r)
{
if(freq[ma].size()==0)
ma-=1;
if(ma==-1||freq[ma].size()==0)
break;
if(!vis2[lis[freq[ma].back().F][freq[ma].back().S].S])
{
lis[node].pb(mp(ma,lis[freq[ma].back().F][freq[ma].back().S].S));
vis2[lis[freq[ma].back().F][freq[ma].back().S].S]=1;
}
ii to=freq[ma].back();
freq[ma].pop_back();
if(lis[to.F].size()-1!=to.S)
freq[lis[to.F][to.S+1].S].pb(mp(to.F,to.S+1));
}
while(mi!=100000001&&ma>mi-1)
{
freq[ma].clear();
ma--;
}
if(lis[node].size()<r)
lis[node].pb(mp(0,node));
for(int i=0;i<lis[node].size();++i)
vis2[lis[node][i].S]=0;
}
void dfs1(int node)
{
ans[node]=0;
vis1[node]=1;
for(int i=0;i<al[node].size();++i)
{
if(vis1[al[node][i]]==0)
{
dfs1(al[node][i]);
}
ans[node]=max(ans[node],1+ans[al[node][i]]);
}
if(ans[node]==0&&vis[node]==1)
ans[node]=-1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,q;
cin>>n>>m>>q;
for(int i=0;i<m;++i)
{
int f,s;
cin>>f>>s;
al[s].pb(f);
}
for(int i=n;i>0;--i)
if(vis1[i]==0)
dfs(i);
vi v;
for(int i=0;i<q;++i)
{
int s,no;
cin>>s>>no;
int an=-1;
for(int j=0;j<no;++j)
{
int f;
cin>>f;
vis[f]=1;
v.pb(f);
}
for(auto j=0;j<lis[s].size();++j)
{
if(vis[lis[s][j].S]==0)
{
an=lis[s][j].F;
break;
}
}
if(an==-1&&no>=r)
{
memset(vis1,0,sizeof(vis1));
dfs1(s);
an=ans[s];
}
for(int j=0;j<v.size();++j)
vis[v[j]]=0;
v.clear();
cout<<an<<'\n';
}
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'void dfs(int)':
bitaro.cpp:27:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
bitaro.cpp:35:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lis[node].size()<r)
~~~~~~~~~~~~~~~~^~
bitaro.cpp:48:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(lis[to.F].size()-1!=to.S)
^
bitaro.cpp:56:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(lis[node].size()<r)
~~~~~~~~~~~~~~~~^~
bitaro.cpp:58:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<lis[node].size();++i)
~^~~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void dfs1(int)':
bitaro.cpp:65:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:104:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(auto j=0;j<lis[s].size();++j)
~^~~~~~~~~~~~~~
bitaro.cpp:118:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<v.size();++j)
~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |