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
typedef pair<int, int> pii;
#define nl '\n'
const int B=300;
int n, q, m;
vector<int> adj[100001];
vector<int> radj[100001];
unordered_map<int, bool> C[100001];
pii best[100001][B+2];
int ind[100001];
int t[100001];
vector<int> batch[100001];
vector<int> sep;
int ans[100001];
int dp[100001];
int used[100001];
int main(){
cin.tie(0)->sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m >> q;
for(int i=1; i<=m; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
radj[v].push_back(u);
}
for(int i=1; i<=q; i++){
int y;
cin >> t[i] >> y;
C[i].reserve(y);
for(int j=1; j<=y; j++) {
int c; cin >> c;
C[i][c]=1;
}
if(y<=B) batch[t[i]].push_back(i);
else sep.push_back(i);
}
for(int i=1; i<=n; i++){
int j=1;
for(int u:radj[i]) ind[u]=1;
while(j<=B+1){
pii use={-1, -1};
for(int u:radj[i]){
while(ind[u]<=B+1 && used[best[u][ind[u]].s]==i) ind[u]++;
if(ind[u]>B+1 || !best[u][ind[u]].s) continue;
use=max(use, best[u][ind[u]]);
}
if(use.f==-1) break;
used[use.s]=i;
best[i][j]={use.f+1, use.s};
j++;
}
if(j<=B+1) best[i][j]={0, i};
for(int j:batch[i]){
int k=1;
while(C[j].find(best[i][k].s)!=C[j].end()) k++;
if(!best[i][k].s) ans[j]=-1;
else ans[j]=best[i][k].f;
}
}
for(int j:sep){
ans[j]=(C[j].find(t[j])==C[j].end())-1;
dp[t[j]]=0;
for(int i=t[j]-1; i>=1; i--){
dp[i]=-1;
for(auto k:adj[i]) if(k<=t[j] && dp[k]!=-1) dp[i]=max(dp[i], dp[k]+1);
if(C[j].find(i)==C[j].end()) ans[j]=max(ans[j], dp[i]);
}
}
for(int i=1; i<=q; i++) cout << ans[i] << '\n';
}
// mp.find
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |