#include <cstdio> #include <vector> #include <algorithm> const int max_n = 401; int n, m, k; std::vector<int> out[max_n]; int deleted[max_n]; int prev_node[max_n]; int dp[max_n]; std::pair<int, int> compute_worst_path() { int curr_max = 0; int curr_index = -1; for (int i = 0; i < n; ++i) { dp[i] = 1; prev_node[i] = -1; for (int j = 0; j < out[i].size(); ++j) { if (deleted[out[i][j]] == 0 && dp[i] < 1 + dp[out[i][j]]) { prev_node[i] = out[i][j]; dp[i] = 1 + dp[out[i][j]]; } } if (curr_max < dp[i]) { curr_max = dp[i]; curr_index = i; } } return std::pair<int, int>(curr_max, curr_index); } int gogogo(int gl) { std::pair<int, int> ret = compute_worst_path(); if (gl == 0) { return ret.first; } else { std::vector<int> candidates; while (ret.second != -1) { candidates.push_back(ret.second); ret.second = prev_node[ret.second]; } int ret = n; for (int a = 0; a < candidates.size(); ++a) { int candidate = candidates[a]; deleted[candidate] = 1; int curr_ret = gogogo(gl - 1); if (curr_ret < ret) ret = curr_ret; deleted[candidate] = 0; } return ret; } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; ++i) { deleted[i] = 0; } for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); --x; --y; out[y].push_back(x); } if (k < n) { printf("%d\n", gogogo(k)); } else { printf("0\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 | #include <cstdio> #include <vector> #include <algorithm> const int max_n = 401; int n, m, k; std::vector<int> out[max_n]; int deleted[max_n]; int prev_node[max_n]; int dp[max_n]; std::pair<int, int> compute_worst_path() { int curr_max = 0; int curr_index = -1; for (int i = 0; i < n; ++i) { dp[i] = 1; prev_node[i] = -1; for (int j = 0; j < out[i].size(); ++j) { if (deleted[out[i][j]] == 0 && dp[i] < 1 + dp[out[i][j]]) { prev_node[i] = out[i][j]; dp[i] = 1 + dp[out[i][j]]; } } if (curr_max < dp[i]) { curr_max = dp[i]; curr_index = i; } } return std::pair<int, int>(curr_max, curr_index); } int gogogo(int gl) { std::pair<int, int> ret = compute_worst_path(); if (gl == 0) { return ret.first; } else { std::vector<int> candidates; while (ret.second != -1) { candidates.push_back(ret.second); ret.second = prev_node[ret.second]; } int ret = n; for (int a = 0; a < candidates.size(); ++a) { int candidate = candidates[a]; deleted[candidate] = 1; int curr_ret = gogogo(gl - 1); if (curr_ret < ret) ret = curr_ret; deleted[candidate] = 0; } return ret; } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; ++i) { deleted[i] = 0; } for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); --x; --y; out[y].push_back(x); } if (k < n) { printf("%d\n", gogogo(k)); } else { printf("0\n"); } return 0; } |