#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; int result = 1000; vector<int> v[305]; int n; int m; int d[305]; bool blocked[305]; vector<int> f() { vector<int> path; int max_dist = 0; int ix = 0; for(int i=n; i>=1; i--) { if(blocked[i]) continue; d[i] = 1; for(int x : v[i]) { if(blocked[x]) continue; d[i] = max(d[i], d[x] + 1); } if(d[i] > max_dist) { max_dist = d[i]; ix = i;} } result = min(result, max_dist); for(int i=0; i<max_dist; i++) { path.push_back(ix); for(int x : v[ix]) if(d[x] + 1 == d[ix]) { ix = x; break; } } return path; } void g(int k) { auto path = f(); if(k == 0) return; for(int x : path) { blocked[x] = 1; g(k-1); blocked[x] = 0; } } int main() { ios::sync_with_stdio(0); int k; cin >> n >> m >> k; for(int i=0; i<m; i++) {int a,b; cin >> a >> b; v[a].push_back(b);} g(k); cout << result << endl; }
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; int result = 1000; vector<int> v[305]; int n; int m; int d[305]; bool blocked[305]; vector<int> f() { vector<int> path; int max_dist = 0; int ix = 0; for(int i=n; i>=1; i--) { if(blocked[i]) continue; d[i] = 1; for(int x : v[i]) { if(blocked[x]) continue; d[i] = max(d[i], d[x] + 1); } if(d[i] > max_dist) { max_dist = d[i]; ix = i;} } result = min(result, max_dist); for(int i=0; i<max_dist; i++) { path.push_back(ix); for(int x : v[ix]) if(d[x] + 1 == d[ix]) { ix = x; break; } } return path; } void g(int k) { auto path = f(); if(k == 0) return; for(int x : path) { blocked[x] = 1; g(k-1); blocked[x] = 0; } } int main() { ios::sync_with_stdio(0); int k; cin >> n >> m >> k; for(int i=0; i<m; i++) {int a,b; cin >> a >> b; v[a].push_back(b);} g(k); cout << result << endl; } |