#include <iostream> #include <vector> #include <bitset> #include <queue> const int MAXN = 305; const int MAXM = 405; int n,m,k; std::vector<int> V[MAXN]; int deg[MAXN]; int K[MAXN]; int calcK(const std::bitset<MAXN> &disabled) { int result = 0; for (int i=1;i<=n;++i) deg[i] = 0; for (int i=1;i<=n;++i) K[i] = 0; for (int i=1;i<=n;++i) { if (!disabled[i]) for (int j : V[i]) ++deg[j]; } std::queue<int> q; for (int i=1;i<=n;++i) if (!disabled[i] && deg[i] == 0) { q.push(i); K[i] = 1; result = 1; } while (!q.empty()) { int i = q.front(); q.pop(); for (int j : V[i]) if (!disabled[j]) { deg[j]--; K[j] = std::max(K[j], K[i]+1); result = std::max(result, K[j]); if (deg[j] == 0) q.push(j); } } return result; } int globalResult = 300; void bruteForce(int d, int i, std::bitset<MAXN> set) { if (d == k) { for (;i<=n;++i) { set[i] = true; globalResult = std::min(globalResult, calcK(set)); set[i] = false; } } else if (d < k) { for (;i<=n;++i) { set[i] = true; bruteForce(d+1, i+1, set); set[i] = false; } } } int main() { std::cin >> n >> m >> k; for (int i=1;i<=n;++i) { int a,b; std::cin >> a >> b; V[a].push_back(b); } globalResult = calcK(0); bruteForce(1, 1, 0); std::cout << globalResult << std::endl; return 0; }
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <iostream> #include <vector> #include <bitset> #include <queue> const int MAXN = 305; const int MAXM = 405; int n,m,k; std::vector<int> V[MAXN]; int deg[MAXN]; int K[MAXN]; int calcK(const std::bitset<MAXN> &disabled) { int result = 0; for (int i=1;i<=n;++i) deg[i] = 0; for (int i=1;i<=n;++i) K[i] = 0; for (int i=1;i<=n;++i) { if (!disabled[i]) for (int j : V[i]) ++deg[j]; } std::queue<int> q; for (int i=1;i<=n;++i) if (!disabled[i] && deg[i] == 0) { q.push(i); K[i] = 1; result = 1; } while (!q.empty()) { int i = q.front(); q.pop(); for (int j : V[i]) if (!disabled[j]) { deg[j]--; K[j] = std::max(K[j], K[i]+1); result = std::max(result, K[j]); if (deg[j] == 0) q.push(j); } } return result; } int globalResult = 300; void bruteForce(int d, int i, std::bitset<MAXN> set) { if (d == k) { for (;i<=n;++i) { set[i] = true; globalResult = std::min(globalResult, calcK(set)); set[i] = false; } } else if (d < k) { for (;i<=n;++i) { set[i] = true; bruteForce(d+1, i+1, set); set[i] = false; } } } int main() { std::cin >> n >> m >> k; for (int i=1;i<=n;++i) { int a,b; std::cin >> a >> b; V[a].push_back(b); } globalResult = calcK(0); bruteForce(1, 1, 0); std::cout << globalResult << std::endl; return 0; } |