#include <algorithm> #include <iostream> #include <vector> class Node { public: std::vector<short> parents; std::vector<short> children; bool removed; Node() : removed(false) { parents.reserve(10); children.reserve(10); } }; int n; int m; int k; std::vector<Node> nodes; std::vector<short> sorted; std::vector<int> scratch; void LoadData() { std::cin >> n >> m >> k; nodes.reserve(n); nodes.resize(n); for (int i = 0; i < m; i++) { short parent, child; std::cin >> parent >> child; nodes.at(parent-1).children.push_back(child-1); nodes.at(child-1).parents.push_back(parent-1); } } void TopoSort() { sorted.reserve(n); std::vector<int> counts(n); for (int i = 0; i < n; i++) { counts[i] = nodes[i].parents.size(); } for (int i = 0; i < n; i++) { bool found = false; for (int j = 0; j < n; j++) { if (counts[j] == 0) { found = true; counts[j] = -1; sorted.push_back(j); for (short child : nodes[j].children) { --counts[child]; } } } if (!found) break; } } int MaxPartialPathLength(std::vector<short> &indices) { int result = 0; for (short i : indices) { int x = scratch[i]; if (x > result) result = x; } return result; } int MaxPathLength() { std::fill(scratch.begin(), scratch.end(), 0); for (short i : sorted) { if (nodes[i].removed) { scratch[i] = 0; } else { scratch[i] = MaxPartialPathLength(nodes[i].parents) + 1; } } return *std::max_element(scratch.begin(), scratch.end()); } int FindBest(int start, int level, int initial) { int result = initial; for (int i = start; i < n - level; i++) { nodes[i].removed = true; int x; if (level > 0) { x = FindBest(i + 1, level - 1, result); } else { x = MaxPathLength(); } if (x < result) result = x; nodes[i].removed = false; } return result; } int main() { LoadData(); TopoSort(); scratch.reserve(n); scratch.resize(n); int result = FindBest(0, k - 1, MaxPathLength()); std::cout << result << '\n'; 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <algorithm> #include <iostream> #include <vector> class Node { public: std::vector<short> parents; std::vector<short> children; bool removed; Node() : removed(false) { parents.reserve(10); children.reserve(10); } }; int n; int m; int k; std::vector<Node> nodes; std::vector<short> sorted; std::vector<int> scratch; void LoadData() { std::cin >> n >> m >> k; nodes.reserve(n); nodes.resize(n); for (int i = 0; i < m; i++) { short parent, child; std::cin >> parent >> child; nodes.at(parent-1).children.push_back(child-1); nodes.at(child-1).parents.push_back(parent-1); } } void TopoSort() { sorted.reserve(n); std::vector<int> counts(n); for (int i = 0; i < n; i++) { counts[i] = nodes[i].parents.size(); } for (int i = 0; i < n; i++) { bool found = false; for (int j = 0; j < n; j++) { if (counts[j] == 0) { found = true; counts[j] = -1; sorted.push_back(j); for (short child : nodes[j].children) { --counts[child]; } } } if (!found) break; } } int MaxPartialPathLength(std::vector<short> &indices) { int result = 0; for (short i : indices) { int x = scratch[i]; if (x > result) result = x; } return result; } int MaxPathLength() { std::fill(scratch.begin(), scratch.end(), 0); for (short i : sorted) { if (nodes[i].removed) { scratch[i] = 0; } else { scratch[i] = MaxPartialPathLength(nodes[i].parents) + 1; } } return *std::max_element(scratch.begin(), scratch.end()); } int FindBest(int start, int level, int initial) { int result = initial; for (int i = start; i < n - level; i++) { nodes[i].removed = true; int x; if (level > 0) { x = FindBest(i + 1, level - 1, result); } else { x = MaxPathLength(); } if (x < result) result = x; nodes[i].removed = false; } return result; } int main() { LoadData(); TopoSort(); scratch.reserve(n); scratch.resize(n); int result = FindBest(0, k - 1, MaxPathLength()); std::cout << result << '\n'; return 0; } |