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 int long long
int N, M, Q;
//vector<pair<int, int>> Canals;
vector<vector<int>> adj;
vector<vector<int>> C;
vector<vector<int>> dp;
int n;
int dfs(int x){
if(dp[n-x][n-x]==0)return 1;
dp[n-x][n-x]=0;
for(auto u : adj[x]){
dfs(u);
dp[n-x][n-u]= max(dp[n-x][n-u], (int)1);
for(int i = x+1; i < N; i++){
if(dp[n-u][n-i]==-1)continue;
dp[n-x][n-i] = max(dp[n-x][n-i], dp[n-u][n-i]+1);
}
}
return 1;
}
signed main(){
cin >> N >> M >> Q;
n=N-1;
//Canals.resize(M);
adj.resize(N);
dp.resize(N, vector<int>(0));
int s, e;
for(int i = 0; i < M; i++){
//cin >> Canals[i].first >> Canals[i].second;
//Canals[i].first--; Canals[i].second--;
//adj[Canals[i].first].push_back(Canals[i].second);
cin >> s >> e;
s--; e--;
adj[s].push_back(e);
}
for(int i = 0; i < N; i++){
dp[n-i].resize(N-i+1, -1);
}
for(int i = 0; i < N; i++){
if(dp[n-i][n-i]!=-1)continue;
dfs(i);
}
/*for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cout << i << " -> " << j << " = " << dp[i][j] << "\n";
}
}*/
int T;
int Y;
C.resize(Q);
vector<bool> TooBusy(N, false);
for(int i = 0; i < Q; i++){
fill(TooBusy.begin(), TooBusy.end(), false); // Replace with changing every Y[i] back at the end
cin >> T >> Y;
T--;
C[i].resize(Y);
for(int j = 0; j < Y; j++){
cin >> C[i][j];
C[i][j]--;
TooBusy[C[i][j]]=true;
}
int ans = -1;
for(int j = 0; j < N; j++){
//cout << i << " && " << T[i] << "\n";
if(TooBusy[j])continue;
ans = max(ans, dp[N-1-j][N-1-T]);
}
cout << ans << "\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... |