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 pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
// #define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double
const int INF = 1e8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int B = 315;
vector<int> adj[MAXN], rev[MAXN];
int dist[MAXN];
bool notok[MAXN];
vector<pair<int, int>> f[MAXN];
bool cmp(int x, int y){
return dist[x] > dist[y];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m, q;
cin>>n>>m>>q;
for(int i= 0;i<m;i++){
int u, v;
cin>>u>>v;
adj[u].pb(v);
rev[v].pb(u);
}
clr(dist, 0);
bool done[n+2];
clr(done, 0);
for(int i=1;i<=n;i++){
vector<int> s;
s.pb(i);
for(int v: rev[i]){
for(auto it: f[v]){
if(!done[it.sc]){
s.pb(it.sc);
done[it.sc] = 1;
}
dist[it.sc] = max(it.fr+1, dist[it.sc]);
}
}
// printvector(s);
int ln = min((int)s.size()-1, B);
nth_element(s.begin(), s.begin()+ln, s.end(), cmp);
// printvector(s);
int curr = 0;
for(int u: s){
if(curr >= B){
break;
}
f[i].pb({dist[u], u});
curr++;
}
for(auto it: s){
dist[it] = 0;
done[it] = 0;
}
}
clr(notok, 0);
while(q--){
int x, y; cin>>x>>y;
vector<int> v;
for(int i= 0;i<y;i++){
int t1; cin>>t1;
v.pb(t1);
notok[t1] =1;
}
if(y >= B){
int dp[n+2], ans = -1;
for(int i=1;i<=n;i++) dp[i] = -INF;
dp[x] = 0;
for(int i= x;i>=1;i--){
for(int v: adj[i]){
dp[i] = max(dp[v]+1, dp[i]);
}
if(notok[i]) continue;
ans = max(ans, dp[i]);
}
cout<<ans<<endl;
} else {
int ans = -1;
for(auto &[d, u]: f[x]){
if(notok[u]) continue;
ans = d;
break;
}
cout<<ans<<endl;
}
for(int i: v){
notok[i] = 0;
}
}
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... |