#include <bits/stdc++.h> using namespace std; vector <int> blocked; vector <vector <int> > G; int n; int rek(int k){ vector <int> dp(n+1, 0); vector <int> path(n+1, 0); vector <int> pat; for(int i = 1; i <= n; ++i){ if(blocked[i])continue; for(int j : G[i]){ if(blocked[j])continue; if(dp[j] + 1 > dp[i]){ path[i] = j; } dp[i] = max(dp[j] + 1, dp[i]); } } int x = 0; for(int i = 1; i <= n; ++i)x = max(dp[i], x); if(k > 0){ for(int i = 1; i <= n; ++i)if(dp[i] == x){ int u = i; pat.push_back(u); while(u != 0){ u = path[u]; pat.push_back(u); } break; } int t = 1e9; for(int g : pat){ blocked[g] = 1; t = min(t, rek(k-1)); blocked[g] = 0; } return t; } return x; } int main(){ ios_base::sync_with_stdio(0); cin >> n; int m; cin >> m; int k; cin >> k; if(k >= n){ cout << 0 << '\n'; return 0; } G.resize(n+1); for(int i = 0; i < m; ++i){ int a, b; cin >> a >> b; G[b].push_back(a); } blocked.resize(n+1,0); cout << rek(k) + 1 << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <bits/stdc++.h> using namespace std; vector <int> blocked; vector <vector <int> > G; int n; int rek(int k){ vector <int> dp(n+1, 0); vector <int> path(n+1, 0); vector <int> pat; for(int i = 1; i <= n; ++i){ if(blocked[i])continue; for(int j : G[i]){ if(blocked[j])continue; if(dp[j] + 1 > dp[i]){ path[i] = j; } dp[i] = max(dp[j] + 1, dp[i]); } } int x = 0; for(int i = 1; i <= n; ++i)x = max(dp[i], x); if(k > 0){ for(int i = 1; i <= n; ++i)if(dp[i] == x){ int u = i; pat.push_back(u); while(u != 0){ u = path[u]; pat.push_back(u); } break; } int t = 1e9; for(int g : pat){ blocked[g] = 1; t = min(t, rek(k-1)); blocked[g] = 0; } return t; } return x; } int main(){ ios_base::sync_with_stdio(0); cin >> n; int m; cin >> m; int k; cin >> k; if(k >= n){ cout << 0 << '\n'; return 0; } G.resize(n+1); for(int i = 0; i < m; ++i){ int a, b; cin >> a >> b; G[b].push_back(a); } blocked.resize(n+1,0); cout << rek(k) + 1 << '\n'; } |