#include <cstdio> #include <cstring> #include <vector> #include <cstdlib> #include <set> #include <algorithm> using namespace std; vector<int> edges[309]; int mx = 0; int cnt = 0; int n, m, k; vector<vector<int>> paths; void dfs(int index, int depth, vector<int>& path) { path.push_back(index); if (depth == 0) { paths.push_back(path); } else { for (auto e: edges[index]) { dfs(e, depth-1, path); } } path.pop_back(); } static char taken[309]; bool backtrack(int remaining, size_t path_index) { if (path_index == paths.size()) { return true; } bool any = false; for (auto p: paths[path_index]) { if (taken[p]) { any = true; break; } } if (any) { return backtrack(remaining, path_index+1); } if (remaining == 0) return false; for (auto p: paths[path_index]) { taken[p] = true; bool r = backtrack(remaining - 1, path_index + 1); taken[p] = false; if (r) return true; } return false; } bool forbidden[309]; int dp[309]; int brucheck() { for (int i=0; i<n; i++) { dp[i] = 1; } int mx = 0; for (int i=0; i<n; i++) { if (forbidden[i]) continue; if (dp[i] > mx) mx = dp[i]; for (auto e: edges[i]) { if (forbidden[e]) continue; if (dp[i] + 1 > dp[e]) dp[e] = dp[i] + 1; } } /* printf("Brucheck ("); for (int i=0; i<n; i++) { if (forbidden[i]) printf("%d, ", i+1); } printf(") -> %d\n", mx); for (int i = 0; i < n; i++) { printf("%d ", dp[i]); } printf("\n"); */ return mx; } int main() { scanf("%d %d %d", &n, &m, &k); vector<pair<int, int>> ed; for (int i=0; i<m; i++) { int x, y; scanf("%d %d", &x, &y); x--; y--; edges[x].push_back(y); ed.push_back({x, y}); } if (n < 75) { int mx = 1e9; mx = min(mx, brucheck()); if (k > 0) for (int i0 = 0; i0 < n; i0++) { forbidden[i0] = true; mx = min(mx, brucheck()); if (k > 1) for (int i1 = i0+1; i1 < n; i1++) { forbidden[i1] = true; mx = min(mx, brucheck()); if (k > 2) for (int i2 = i1+1; i2 < n; i2++) { forbidden[i2] = true; mx = min(mx, brucheck()); if (k > 3) for (int i3 = i2+1; i3 < n; i3++) { forbidden[i3] = true; mx = min(mx, brucheck()); forbidden[i3] = false; } forbidden[i2] = false; } forbidden[i1] = false; } forbidden[i0] = false; } printf("%d\n", mx); return 0; } for (int check = 1; ; check++) { paths.clear(); for (int i=0; i<n; i++) { vector<int> v; dfs(i, check, v); } set<vector<int>> comp(paths.begin(), paths.end()); paths = vector<vector<int>> (comp.begin(), comp.end()); sort(paths.begin(), paths.end(), [](vector<int>a, vector<int>b){ return a.size() < b.size(); }); bool res = backtrack(k, 0); if (res) { printf("%d\n", check); break; } } }
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 | #include <cstdio> #include <cstring> #include <vector> #include <cstdlib> #include <set> #include <algorithm> using namespace std; vector<int> edges[309]; int mx = 0; int cnt = 0; int n, m, k; vector<vector<int>> paths; void dfs(int index, int depth, vector<int>& path) { path.push_back(index); if (depth == 0) { paths.push_back(path); } else { for (auto e: edges[index]) { dfs(e, depth-1, path); } } path.pop_back(); } static char taken[309]; bool backtrack(int remaining, size_t path_index) { if (path_index == paths.size()) { return true; } bool any = false; for (auto p: paths[path_index]) { if (taken[p]) { any = true; break; } } if (any) { return backtrack(remaining, path_index+1); } if (remaining == 0) return false; for (auto p: paths[path_index]) { taken[p] = true; bool r = backtrack(remaining - 1, path_index + 1); taken[p] = false; if (r) return true; } return false; } bool forbidden[309]; int dp[309]; int brucheck() { for (int i=0; i<n; i++) { dp[i] = 1; } int mx = 0; for (int i=0; i<n; i++) { if (forbidden[i]) continue; if (dp[i] > mx) mx = dp[i]; for (auto e: edges[i]) { if (forbidden[e]) continue; if (dp[i] + 1 > dp[e]) dp[e] = dp[i] + 1; } } /* printf("Brucheck ("); for (int i=0; i<n; i++) { if (forbidden[i]) printf("%d, ", i+1); } printf(") -> %d\n", mx); for (int i = 0; i < n; i++) { printf("%d ", dp[i]); } printf("\n"); */ return mx; } int main() { scanf("%d %d %d", &n, &m, &k); vector<pair<int, int>> ed; for (int i=0; i<m; i++) { int x, y; scanf("%d %d", &x, &y); x--; y--; edges[x].push_back(y); ed.push_back({x, y}); } if (n < 75) { int mx = 1e9; mx = min(mx, brucheck()); if (k > 0) for (int i0 = 0; i0 < n; i0++) { forbidden[i0] = true; mx = min(mx, brucheck()); if (k > 1) for (int i1 = i0+1; i1 < n; i1++) { forbidden[i1] = true; mx = min(mx, brucheck()); if (k > 2) for (int i2 = i1+1; i2 < n; i2++) { forbidden[i2] = true; mx = min(mx, brucheck()); if (k > 3) for (int i3 = i2+1; i3 < n; i3++) { forbidden[i3] = true; mx = min(mx, brucheck()); forbidden[i3] = false; } forbidden[i2] = false; } forbidden[i1] = false; } forbidden[i0] = false; } printf("%d\n", mx); return 0; } for (int check = 1; ; check++) { paths.clear(); for (int i=0; i<n; i++) { vector<int> v; dfs(i, check, v); } set<vector<int>> comp(paths.begin(), paths.end()); paths = vector<vector<int>> (comp.begin(), comp.end()); sort(paths.begin(), paths.end(), [](vector<int>a, vector<int>b){ return a.size() < b.size(); }); bool res = backtrack(k, 0); if (res) { printf("%d\n", check); break; } } } |