#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
#define pb push_back
#define fst first
#define snd second
using namespace std;
vector<vector<int>> adj;
vector<vector<pair<int,int>>> yth;
vector<pair<int,int>> aux;
vector<int> vst;
void merge(int i, int j) {
aux.clear();
int l=0, maxl = yth[i].size()-1;
int r=0, maxr = yth[j].size()-1;
vst.clear(), vst.resize(i+1,0);
int f, s;
while(aux.size()<=301) {
if(l<=maxl and r<=maxr) {
if(yth[i][l].fst > yth[j][r].fst) {
f=yth[i][l].fst, s=yth[i][l].snd;
if(!vst[s]) {
vst[s]=1;
aux.pb({f,s});
}
l++;
} else {
f=yth[j][r].fst, s=yth[j][r].snd;
if(!vst[s]) {
vst[s]=1;
aux.pb({f+1,s});
}
r++;
}
} else if(l<=maxl) {
f=yth[i][l].fst, s=yth[i][l].snd;
if(!vst[s]) {
vst[s]=1;
aux.pb({f,s});
}
l++;
} else if(r<=maxr) {
f=yth[j][r].fst, s=yth[j][r].snd;
if(!vst[s]) {
vst[s]=1;
aux.pb({f+1,s});
}
r++;
} else break;
}
yth[i]=aux;
}
void precalc(int n) {
for(int x=1; x<=n; ++x) {
if(yth[x].empty()) yth[x].pb({0,x});
for(int y : adj[x]) merge(x,y);
}
}
vector<int> busy;
int solve(int t) {
for(int i=0; i<=(yth[t].size()-1); ++i) {
if(!busy[yth[t][i].snd]) return yth[t][i].fst;
}
return -1;
}
vector<int> d;
int brute(int t) {
d.clear(), d.resize(t+1,0);
for(int x=1; x<t; ++x) {
for(int y : adj[x]) d[x]=max(d[x],d[y]+1);
}
for(int x : adj[t]){
if(busy[x]) continue;
d[t]=max(d[t],d[x]+1);
}
return d[t];
}
int32_t main() {
int n, m, q; cin >> n >> m >> q;
adj.resize(n+1); yth.resize(n+1);
int a, b;
while(m-- and cin >> a >> b) adj[b].pb(a);
precalc(n);
int t, y;
while(cin >> t >> y) {
busy.clear(), busy.resize(n+1,0);
for(int i=0; i<y; ++i) {
cin >> a;
busy[a]=1;
}
if(y<=300) cout << solve(t) << endl;
else cout << brute(t) << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |