#include <cstdio> #include <vector> using namespace std; int n, m, k, u, v, result=10000, dist[305], nextv[305], longest[305], start[305]; vector<int> graph[305], paths[4]; bool disabled[305]; void update(int x) { for (int i = x; i > 0; i--) { if (disabled[i]) continue; dist[i] = 0; for (int v : graph[i]) { if (disabled[v]) continue; if (dist[v] >= dist[i]) { dist[i] = dist[v] + 1; nextv[i] = v; } } longest[i] = longest[i+1]; start[i] = start[i+1]; if (dist[i] > longest[i]) { longest[i] = dist[i]; start[i] = i; } } } void compute(int x, int to_delete) { //fill(dist, dist+n+1, 0); //fill(nextv, nextv+n+1, 0); disabled[x] = true; update(x); if (to_delete == 0) { result = min(result, longest[1]); } else { auto &path = paths[k - to_delete]; path.clear(); v = start[1]; while (v != 0) { path.push_back(v); v = nextv[v]; } for (auto v : path) { compute(v, to_delete - 1); } } disabled[x] = false; update(x); } int main() { scanf("%d%d%d", &n, &m, &k); while (m--) { scanf("%d%d", &u, &v); graph[u].push_back(v); } if (n==k) { printf("0\n"); return 0; } compute(n+1, k); printf("%d\n", result+1); }
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 | #include <cstdio> #include <vector> using namespace std; int n, m, k, u, v, result=10000, dist[305], nextv[305], longest[305], start[305]; vector<int> graph[305], paths[4]; bool disabled[305]; void update(int x) { for (int i = x; i > 0; i--) { if (disabled[i]) continue; dist[i] = 0; for (int v : graph[i]) { if (disabled[v]) continue; if (dist[v] >= dist[i]) { dist[i] = dist[v] + 1; nextv[i] = v; } } longest[i] = longest[i+1]; start[i] = start[i+1]; if (dist[i] > longest[i]) { longest[i] = dist[i]; start[i] = i; } } } void compute(int x, int to_delete) { //fill(dist, dist+n+1, 0); //fill(nextv, nextv+n+1, 0); disabled[x] = true; update(x); if (to_delete == 0) { result = min(result, longest[1]); } else { auto &path = paths[k - to_delete]; path.clear(); v = start[1]; while (v != 0) { path.push_back(v); v = nextv[v]; } for (auto v : path) { compute(v, to_delete - 1); } } disabled[x] = false; update(x); } int main() { scanf("%d%d%d", &n, &m, &k); while (m--) { scanf("%d%d", &u, &v); graph[u].push_back(v); } if (n==k) { printf("0\n"); return 0; } compute(n+1, k); printf("%d\n", result+1); } |