#include <bits/stdc++.h> const int MAX_N = 300; int n, m, k; std::vector<int> list[MAX_N+3]; int res; void input(){ std::cin >> n >> m >> k; int a, b; for (int i = 1; i <= m; i++){ std::cin >> a >> b; list[a-1].push_back(b-1); } } int dp[MAX_N+3]; int getRes(std::vector<int> &t){ for (int i = 0; i < n; i++) dp[i] = 1; for (int i = 0; i < n; i++) if (t[i]) dp[i] = 0; for (int i = n-1; i >= 0; i--){ if (t[i]) continue; for (auto u: list[i]) if (!t[u]) dp[i] = std::max(dp[i], dp[u] + 1); if (dp[i] >= res) return dp[i]; } int res = 0; for (int i = 0; i < n; i++) res = std::max(res, dp[i]); return res; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); std::vector<int> t; for (int i = 1; i <= n-k; i++) t.push_back(0); for (int i = 1; i <= k; i++) t.push_back(1); res = INT_MAX; do{ res = std::min(res, getRes(t)); } while (std::next_permutation(t.begin(), t.end())); std::cout << res << "\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 | #include <bits/stdc++.h> const int MAX_N = 300; int n, m, k; std::vector<int> list[MAX_N+3]; int res; void input(){ std::cin >> n >> m >> k; int a, b; for (int i = 1; i <= m; i++){ std::cin >> a >> b; list[a-1].push_back(b-1); } } int dp[MAX_N+3]; int getRes(std::vector<int> &t){ for (int i = 0; i < n; i++) dp[i] = 1; for (int i = 0; i < n; i++) if (t[i]) dp[i] = 0; for (int i = n-1; i >= 0; i--){ if (t[i]) continue; for (auto u: list[i]) if (!t[u]) dp[i] = std::max(dp[i], dp[u] + 1); if (dp[i] >= res) return dp[i]; } int res = 0; for (int i = 0; i < n; i++) res = std::max(res, dp[i]); return res; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); std::vector<int> t; for (int i = 1; i <= n-k; i++) t.push_back(0); for (int i = 1; i <= k; i++) t.push_back(1); res = INT_MAX; do{ res = std::min(res, getRes(t)); } while (std::next_permutation(t.begin(), t.end())); std::cout << res << "\n"; } |