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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=1e5+9;
vector<int>a[maxn];
vector<pii>b[maxn];
bool blk[maxn];
int f[maxn];
int n,m;
int brute(int u,vector<int> &block){
for1(i,1,n){
blk[i]=0;
}
for (auto v:block)blk[v]=1;
for1(i,1,u){
f[i]=0;
if (blk[i])f[i]=-2*n-1;
for (auto v:a[i]){
f[i]=max(f[i],f[v]+1);
}
}
if (f[u]<0)f[u]=-1;
return f[u];
}
const int block=150;
bool use[maxn];
vector<pii> merge(vector<pii> p, vector<pii> q){
vector<pii>r;
reverse(all(p));
reverse(all(q));
for (auto &v:q){
v.se++;
}
while (!p.empty()&&!q.empty()){
if (sz(r)==block){
break;
}
auto u=p.back(),v=q.back();
if (use[u.fi]){
p.pop_back();
continue;
}
if (use[v.fi]){
q.pop_back();
continue;
}
if (u.fi==v.fi&&u.se==v.se){
r.pb(u);
use[u.fi]=1;
p.pop_back();
q.pop_back();
continue;
}
if (u.fi==v.fi){
if (u.se>=v.se){
r.pb(u);
use[u.fi]=1;
p.pop_back();
q.pop_back();
}
else {
r.pb(v);
use[v.fi]=1;
p.pop_back();
q.pop_back();
}
continue;
}
if (u.se>v.se){
r.pb(u);
use[u.fi]=1;
p.pop_back();
}
else {
r.pb(v);
use[v.fi]=1;
q.pop_back();
}
}
while (sz(r)>block){
use[r.back().fi]=0;
r.pop_back();
}
while (!p.empty()){
if (sz(r)==block)break;
if (use[p.back().fi]){
p.pop_back();
continue;
}
r.pb(p.back());
use[p.back().fi]=1;
p.pop_back();
}
while (!q.empty()){
if (sz(r)==block)break;
if (use[q.back().fi]){
q.pop_back();
continue;
}
use[q.back().fi]=1;
r.pb(q.back());
q.pop_back();
}
for (auto v:r)use[v.fi]=0;
return r;
}
void solve(){
for1(i,1,n){
b[i].pb({i,0});
for (auto u:a[i]){
b[i]=merge(b[i],b[u]);
}
}
}
int an[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int q;
cin>>n>>m>>q;
for1(i,1,m){
int u,v;
cin>>u>>v;
a[v].pb(u);
}
solve();
for1(i,1,q){
int x,y;
cin>>x>>y;
vector<int>blocked;
for1(j,1,y){
int x1;
cin>>x1;
blocked.pb(x1);
}
if (y>block){
an[i]=brute(x,blocked);
}
else {
for (auto v:blocked){
use[v]=1;
}
int ans=-1;
for(auto v:b[x]){
if (!use[v.fi]){
//cout<<v.fi<<'\n';
ans=max(ans,v.se);
}
}
an[i]=ans;
}
for (auto v:blocked){
use[v]=0;
}
}
for1(i,1,q){
cout<<an[i]<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |