#include <algorithm> #include <cstdio> #include <numeric> #include <unordered_set> #include <vector> #include <queue> #include <utility> #define ADD_EDGE(g, x, y) \ do {\ g[x].succs.insert(y);\ g[y].preds.insert(x);\ } while (0); #define REMOVE_EDGE(g, x, y) \ do {\ g[x].succs.erase(y);\ g[y].preds.erase(x);\ } while (0); struct vertex_st { std::unordered_set<int> succs, preds; int dist1{}, dist2{}, path{}; bool is_active = true; }; using LL = long long; using pair_ii = std::pair<int, int>; using Graph = std::vector<vertex_st>; /* LL factorial(int n) { LL res = 1; for (int i = 2; i <= n; i++) res *= i; return res; } */ LL choose(int n, int k) { LL res = 1; for (int i = n - k + 1; i <= n; i++) res *= i; for (int i = 2; i <= k; i++) res /= i; return res; } bool next_combination(int c[], int n, int k) { int i = k - 1, j = -1, m = n; while (i >= 0) { if (c[i] < m) { j = i; break; } --i, --m; } if (j == -1) return false; ++c[j]; ++j; while (j < k) c[j] = c[j - 1] + 1, j++; return true; } int find_longest_path(const Graph& graph) { int n = static_cast<int>(graph.size()) - 1; std::vector<int> paths(graph.size(), 1); for (int i = 1; i <= n; i++) { if (!graph[i].is_active) paths[i] = 0; } for (int i = n; i > 0; i--) { if (!graph[i].is_active) continue; for (auto v : graph[i].succs) { if (graph[v].is_active) { paths[i] = std::max(paths[i], paths[v] + 1); } } } return *std::max_element(begin(paths) + 1, end(paths)); } void compute_paths(Graph& graph) { int n = static_cast<int>(graph.size() - 1); for (int u = 1; u <= n; u++) { graph[u].dist1 = 1; graph[u].dist2 = 1; graph[u].path = 0; } for (int u = n; u > 0; u--) { if (!graph[u].is_active) continue; for (int v : graph[u].succs) { if (graph[v].is_active) { graph[u].dist1 = std::max(graph[u].dist1, graph[v].dist1 + 1); } } } for (int u = 1; u <= n; u++) { if (!graph[u].is_active) continue; for (int v : graph[u].preds) { if (graph[v].is_active) { graph[u].dist2 = std::max(graph[u].dist2, graph[v].dist2 + 1); } } } for (int v = 1; v <= n; v++) { if (graph[v].is_active) { graph[v].path = graph[v].dist1 + graph[v].dist2 - 1; } } } int solve(Graph& graph, int k) { compute_paths(graph); int n = static_cast<int>(graph.size() - 1); std::vector<pair_ii> vertices; for (int v = 1; v <= n; v++) { if (graph[v].is_active) { vertices.push_back({ graph[v].path, v }); } } if (vertices.empty()) return 0; int max_path = std::max_element(begin(vertices), end(vertices), [](const pair_ii& x, const pair_ii& y) { return x.first < y.first; })->first; if (k == 0) return max_path; int result = max_path; if (choose(n, k) <= 25000) { int n = static_cast<int>(graph.size() - 1); int c[4]; std::iota(c, c + k, 1); do { bool skip = false; for (int i = 0; i < k; i++) if (!graph[c[i]].is_active) skip = true; if (skip) continue; for (int i = 0; i < k; i++) graph[c[i]].is_active = false; result = std::min(result, find_longest_path(graph)); for (int i = 0; i < k; i++) graph[c[i]].is_active = true; } while (next_combination(c, n, k)); } else { for (pair_ii& x : vertices) { if (x.first == max_path && graph[x.second].is_active) { graph[x.second].is_active = false; result = std::min(result, solve(graph, k - 1)); graph[x.second].is_active = true; } } } return result; } int main() { int n, m, k; std::scanf("%i %i %i", &n, &m, &k); Graph graph(n + 1); for (int i = 0; i < m; i++) { int x, y; std::scanf("%i %i", &x, &y); ADD_EDGE(graph, x, y); } graph[0].is_active = false; std::printf("%i\n", solve(graph, k)); }
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <algorithm> #include <cstdio> #include <numeric> #include <unordered_set> #include <vector> #include <queue> #include <utility> #define ADD_EDGE(g, x, y) \ do {\ g[x].succs.insert(y);\ g[y].preds.insert(x);\ } while (0); #define REMOVE_EDGE(g, x, y) \ do {\ g[x].succs.erase(y);\ g[y].preds.erase(x);\ } while (0); struct vertex_st { std::unordered_set<int> succs, preds; int dist1{}, dist2{}, path{}; bool is_active = true; }; using LL = long long; using pair_ii = std::pair<int, int>; using Graph = std::vector<vertex_st>; /* LL factorial(int n) { LL res = 1; for (int i = 2; i <= n; i++) res *= i; return res; } */ LL choose(int n, int k) { LL res = 1; for (int i = n - k + 1; i <= n; i++) res *= i; for (int i = 2; i <= k; i++) res /= i; return res; } bool next_combination(int c[], int n, int k) { int i = k - 1, j = -1, m = n; while (i >= 0) { if (c[i] < m) { j = i; break; } --i, --m; } if (j == -1) return false; ++c[j]; ++j; while (j < k) c[j] = c[j - 1] + 1, j++; return true; } int find_longest_path(const Graph& graph) { int n = static_cast<int>(graph.size()) - 1; std::vector<int> paths(graph.size(), 1); for (int i = 1; i <= n; i++) { if (!graph[i].is_active) paths[i] = 0; } for (int i = n; i > 0; i--) { if (!graph[i].is_active) continue; for (auto v : graph[i].succs) { if (graph[v].is_active) { paths[i] = std::max(paths[i], paths[v] + 1); } } } return *std::max_element(begin(paths) + 1, end(paths)); } void compute_paths(Graph& graph) { int n = static_cast<int>(graph.size() - 1); for (int u = 1; u <= n; u++) { graph[u].dist1 = 1; graph[u].dist2 = 1; graph[u].path = 0; } for (int u = n; u > 0; u--) { if (!graph[u].is_active) continue; for (int v : graph[u].succs) { if (graph[v].is_active) { graph[u].dist1 = std::max(graph[u].dist1, graph[v].dist1 + 1); } } } for (int u = 1; u <= n; u++) { if (!graph[u].is_active) continue; for (int v : graph[u].preds) { if (graph[v].is_active) { graph[u].dist2 = std::max(graph[u].dist2, graph[v].dist2 + 1); } } } for (int v = 1; v <= n; v++) { if (graph[v].is_active) { graph[v].path = graph[v].dist1 + graph[v].dist2 - 1; } } } int solve(Graph& graph, int k) { compute_paths(graph); int n = static_cast<int>(graph.size() - 1); std::vector<pair_ii> vertices; for (int v = 1; v <= n; v++) { if (graph[v].is_active) { vertices.push_back({ graph[v].path, v }); } } if (vertices.empty()) return 0; int max_path = std::max_element(begin(vertices), end(vertices), [](const pair_ii& x, const pair_ii& y) { return x.first < y.first; })->first; if (k == 0) return max_path; int result = max_path; if (choose(n, k) <= 25000) { int n = static_cast<int>(graph.size() - 1); int c[4]; std::iota(c, c + k, 1); do { bool skip = false; for (int i = 0; i < k; i++) if (!graph[c[i]].is_active) skip = true; if (skip) continue; for (int i = 0; i < k; i++) graph[c[i]].is_active = false; result = std::min(result, find_longest_path(graph)); for (int i = 0; i < k; i++) graph[c[i]].is_active = true; } while (next_combination(c, n, k)); } else { for (pair_ii& x : vertices) { if (x.first == max_path && graph[x.second].is_active) { graph[x.second].is_active = false; result = std::min(result, solve(graph, k - 1)); graph[x.second].is_active = true; } } } return result; } int main() { int n, m, k; std::scanf("%i %i %i", &n, &m, &k); Graph graph(n + 1); for (int i = 0; i < m; i++) { int x, y; std::scanf("%i %i", &x, &y); ADD_EDGE(graph, x, y); } graph[0].is_active = false; std::printf("%i\n", solve(graph, k)); } |