#include <bits/stdc++.h>
using namespace std;
const int buc=370;
int n,m,q;
vector<int> adj[100005];
vector<pair<int,int> > best[100005];
int v[100005],got[100005];
vector<pair<int,int> > comb;
void dfs(int x){
if(v[x]) return;
best[x].push_back({0,x});
for(int i:adj[x]){
dfs(i);
int c1=0,c2=0;
while((c1<(int)best[x].size()||c2<(int)best[i].size())&&(int)comb.size()<buc){
if(c1==(int)best[x].size()){
if(!got[best[i][c2].second]){
comb.push_back(best[i][c2]);
comb.back().first++;
}
c2++;
}
else if(c2==(int)best[i].size()){
if(!got[best[x][c1].second]) comb.push_back(best[x][c1]);
c1++;
}
else if(best[x][c1].first>best[i][c2].first+1){
if(!got[best[x][c1].second]) comb.push_back(best[x][c1]);
c1++;
}
else{
if(!got[best[i][c2].second]){
comb.push_back(best[i][c2]);
comb.back().first++;
}
c2++;
}
if(!comb.empty()) got[comb.back().second]=1;
}
for(auto j:comb) got[j.second]=0;
swap(best[x],comb);
comb.clear();
}
}
int ded[100005],dp[100005];
int dfs2(int x){
if(v[x]) return dp[x];
int ret=0;
if(ded[x]) ret=-1;
for(int i:adj[x]){
int hm=dfs2(i);
if(hm>-1) ret=max(ret,hm+1);
}
return dp[x]=ret;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for(int i=0; i<m; i++){
int a,b;
cin >> a >> b;
adj[b].push_back(a);
}
for(int i=1; i<=n; i++) dfs(i);
while(q--){
int x,num;
cin >> x >> num;
vector<int> undo;
for(int i=0; i<num; i++){
int a;
cin >> a;
undo.push_back(a);
ded[a]=1;
}
if(num<buc){
bool got=false;
for(auto i:best[x]){
if(!ded[i.second]){
cout << i.first << '\n';
got=true;
break;
}
}
if(!got){
cout << -1 << '\n';
}
}
else{
for(int i=0; i<n; i++) v[i]=0,dp[i]=-1;
cout << dfs2(x) << '\n';
}
for(int i:undo) ded[i]=0;
undo.clear();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |