#include <bits/stdc++.h> using namespace std; struct Graph { int n; vector<int> pre; vector<vector<int> > suc; vector<int> erased; vector<int> unerasable; void init(int nn) { n = nn; pre.resize(n, 0); suc.resize(n); erased.resize(n, 0); unerasable.resize(n, 0); } void addEdge(int a, int b) { for (int i=0; i<suc[a].size(); ++i) if (suc[a][i] == b) return; suc[a].push_back(b); ++pre[b]; } void eraseVertex(int a) { erased[a] = 1; for (int i=0; i<suc[a].size(); ++i) --pre[suc[a][i]]; } void restoreVertex(int a) { erased[a] = 0; for (int i=0; i<suc[a].size(); ++i) ++pre[suc[a][i]]; } int diag(vector<int> &path, bool buildPath = true) { vector<int> cnt(pre); queue<int> q; for (int i=0; i<n; ++i) if (cnt[i] == 0 && erased[i] == 0) q.push(i); vector<int> dist(n, 1); vector<int> prev(n, -1); int res = 0; int last = 0; while (!q.empty()) { int x = q.front(); q.pop(); if (dist[x] > res) { res = dist[x]; last = x; } for (int k=0; k<suc[x].size(); ++k) { int y = suc[x][k]; if (erased[y]) continue; if (dist[x] + 1 > dist[y]) { dist[y] = dist[x] + 1; prev[y] = x; } --cnt[y]; if (cnt[y] == 0) q.push(y); } } if (buildPath) { while (last >= 0) { path.push_back(last); last = prev[last]; } } return res; } }; Graph g; int solve(int k) { vector<int> path; if (k == 0) return g.diag(path, false); g.diag(path); int res = 1000; int cur; int i = path.size() / 2; int j = i + 1; int x; for (; i>=0 || j<path.size(); --i, ++j) { if (i >= 0) { x = path[i]; ++g.unerasable[x]; if (g.unerasable[x] == 1) { g.eraseVertex(x); cur = solve(k-1); if (cur < res) res = cur; g.restoreVertex(x); } } if (j < path.size()) { x = path[j]; ++g.unerasable[x]; if (g.unerasable[x] == 1) { g.eraseVertex(x); cur = solve(k-1); if (cur < res) res = cur; g.restoreVertex(x); } } } for (int i=0; i<path.size(); ++i) --g.unerasable[path[i]]; return res; } int read() { int n; scanf("%d", &n); return n; } int mainSolve() { int n = read(); int m = read(); int k = read(); if (k >= n) return 0; g.init(n); for (int i=0; i<m; ++i) { int a = read() - 1; int b = read() - 1; g.addEdge(a, b); } int res = solve(k); return res; } int main() { int res = mainSolve(); printf("%d\n", res); 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 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 | #include <bits/stdc++.h> using namespace std; struct Graph { int n; vector<int> pre; vector<vector<int> > suc; vector<int> erased; vector<int> unerasable; void init(int nn) { n = nn; pre.resize(n, 0); suc.resize(n); erased.resize(n, 0); unerasable.resize(n, 0); } void addEdge(int a, int b) { for (int i=0; i<suc[a].size(); ++i) if (suc[a][i] == b) return; suc[a].push_back(b); ++pre[b]; } void eraseVertex(int a) { erased[a] = 1; for (int i=0; i<suc[a].size(); ++i) --pre[suc[a][i]]; } void restoreVertex(int a) { erased[a] = 0; for (int i=0; i<suc[a].size(); ++i) ++pre[suc[a][i]]; } int diag(vector<int> &path, bool buildPath = true) { vector<int> cnt(pre); queue<int> q; for (int i=0; i<n; ++i) if (cnt[i] == 0 && erased[i] == 0) q.push(i); vector<int> dist(n, 1); vector<int> prev(n, -1); int res = 0; int last = 0; while (!q.empty()) { int x = q.front(); q.pop(); if (dist[x] > res) { res = dist[x]; last = x; } for (int k=0; k<suc[x].size(); ++k) { int y = suc[x][k]; if (erased[y]) continue; if (dist[x] + 1 > dist[y]) { dist[y] = dist[x] + 1; prev[y] = x; } --cnt[y]; if (cnt[y] == 0) q.push(y); } } if (buildPath) { while (last >= 0) { path.push_back(last); last = prev[last]; } } return res; } }; Graph g; int solve(int k) { vector<int> path; if (k == 0) return g.diag(path, false); g.diag(path); int res = 1000; int cur; int i = path.size() / 2; int j = i + 1; int x; for (; i>=0 || j<path.size(); --i, ++j) { if (i >= 0) { x = path[i]; ++g.unerasable[x]; if (g.unerasable[x] == 1) { g.eraseVertex(x); cur = solve(k-1); if (cur < res) res = cur; g.restoreVertex(x); } } if (j < path.size()) { x = path[j]; ++g.unerasable[x]; if (g.unerasable[x] == 1) { g.eraseVertex(x); cur = solve(k-1); if (cur < res) res = cur; g.restoreVertex(x); } } } for (int i=0; i<path.size(); ++i) --g.unerasable[path[i]]; return res; } int read() { int n; scanf("%d", &n); return n; } int mainSolve() { int n = read(); int m = read(); int k = read(); if (k >= n) return 0; g.init(n); for (int i=0; i<m; ++i) { int a = read() - 1; int b = read() - 1; g.addEdge(a, b); } int res = solve(k); return res; } int main() { int res = mainSolve(); printf("%d\n", res); return 0; } |