#include <bits/stdc++.h> using namespace std; #define pb push_back const int ac = 1e3; int n, m, k; vector<vector<int> > I; int D[ac]; int O[ac]; int X[4]; int W[ac]; int solve() { int m = 0; for (int i = n - 1; i >= 0; i--) { bool b = 0; for (int j = 0; j < k; j++) if (X[j] == O[i]) { b = 1; break; } if (b) { W[O[i]] = 0; continue; } W[O[i]] = 1; for (int j = 0; j < I[O[i]].size(); j++) W[O[i]] = max(W[O[i]], W[I[O[i]][j]] + 1); m = max(m, W[O[i]]); } return m; } int gen(int c = 0, int b = 0) { if (c == k) return solve(); int w = 1e9; for (int i = b; i < n; i++) X[c] = i, w = min(w, gen(c + 1, i + 1)); return w; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; I.resize(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; I[a].pb(b); D[b]++; } vector<int> Q; for (int i = 0; i < n; i++) if (!D[i]) Q.pb(i); int j = 0; while (j < Q.size()) { O[j] = Q[j]; for (int i = 0; i < I[Q[j]].size(); i++) { D[I[Q[j]][i]]--; if (!D[I[Q[j]][i]]) Q.pb(I[Q[j]][i]); } j++; } cout << gen() << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; #define pb push_back const int ac = 1e3; int n, m, k; vector<vector<int> > I; int D[ac]; int O[ac]; int X[4]; int W[ac]; int solve() { int m = 0; for (int i = n - 1; i >= 0; i--) { bool b = 0; for (int j = 0; j < k; j++) if (X[j] == O[i]) { b = 1; break; } if (b) { W[O[i]] = 0; continue; } W[O[i]] = 1; for (int j = 0; j < I[O[i]].size(); j++) W[O[i]] = max(W[O[i]], W[I[O[i]][j]] + 1); m = max(m, W[O[i]]); } return m; } int gen(int c = 0, int b = 0) { if (c == k) return solve(); int w = 1e9; for (int i = b; i < n; i++) X[c] = i, w = min(w, gen(c + 1, i + 1)); return w; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; I.resize(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; I[a].pb(b); D[b]++; } vector<int> Q; for (int i = 0; i < n; i++) if (!D[i]) Q.pb(i); int j = 0; while (j < Q.size()) { O[j] = Q[j]; for (int i = 0; i < I[Q[j]].size(); i++) { D[I[Q[j]][i]]--; if (!D[I[Q[j]][i]]) Q.pb(I[Q[j]][i]); } j++; } cout << gen() << '\n'; } |