#include <bits/stdc++.h> using namespace std; vector<vector<int>> v(301); int best(vector<vector<int>>& v, vector<int>& deg, vector<int>& dist, vector<int>& from, int n){ queue<int> q; for(int i = 1; i <= n; i++){ if(deg[i] == 0){ from[i] = -1; q.push(i); } } int best = 0; while(!q.empty()){ int f = q.front(); q.pop(); for(int x: v[f]){ if(dist[x] < dist[f] + 1){ dist[x] = dist[f] + 1; from[x] = f; } if(dist[x] > dist[best]){ best = x; } deg[x]--; if(deg[x] == 0){ q.push(x); } } } return best; } vector<int> get_path(vector<int>& from, int start){ vector<int> result; while(start != -1){ result.push_back(start); start = from[start]; } return result; } //int cnt = 0; int result(vector<vector<int>>& v, int n, int k){ /* cnt++; if(cnt % 1000 == 0){ cout<<cnt<<endl; }*/ vector<int> deg(n+1, 0), dist(n+1, 0), from(n+1); for(int i = 1; i <= n; i++){ for(int x : v[i]){ deg[x]++; } } int b = best(v, deg, dist, from, n); if(b == 0){ return 1; } if(k == 0){ return(dist[b] + 1); } vector<int> path = get_path(from, b); int res = 500; for(int i = 0; i < path.size(); i++){ int x = path[i]; //cout<<k<<" "<<x<<endl; vector<vector<int>> v_temp(n+1); for(int i = 1; i <= n; i++){ if(i != x){ for(int j = 0; j < (int)v[i].size(); j++){ if(v[i][j] != x){ v_temp[i].push_back(v[i][j]); } } } } res = min(res, result(v_temp, n, k-1)); } return res; } int main(){ int n, m, k; cin>>n>>m>>k; for(int i = 0; i < m; i++){ int x, y; cin>>x>>y; v[x].push_back(y); } if(n == k){ cout<<0<<endl; return 0; } cout<<result(v, n, k)<<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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <bits/stdc++.h> using namespace std; vector<vector<int>> v(301); int best(vector<vector<int>>& v, vector<int>& deg, vector<int>& dist, vector<int>& from, int n){ queue<int> q; for(int i = 1; i <= n; i++){ if(deg[i] == 0){ from[i] = -1; q.push(i); } } int best = 0; while(!q.empty()){ int f = q.front(); q.pop(); for(int x: v[f]){ if(dist[x] < dist[f] + 1){ dist[x] = dist[f] + 1; from[x] = f; } if(dist[x] > dist[best]){ best = x; } deg[x]--; if(deg[x] == 0){ q.push(x); } } } return best; } vector<int> get_path(vector<int>& from, int start){ vector<int> result; while(start != -1){ result.push_back(start); start = from[start]; } return result; } //int cnt = 0; int result(vector<vector<int>>& v, int n, int k){ /* cnt++; if(cnt % 1000 == 0){ cout<<cnt<<endl; }*/ vector<int> deg(n+1, 0), dist(n+1, 0), from(n+1); for(int i = 1; i <= n; i++){ for(int x : v[i]){ deg[x]++; } } int b = best(v, deg, dist, from, n); if(b == 0){ return 1; } if(k == 0){ return(dist[b] + 1); } vector<int> path = get_path(from, b); int res = 500; for(int i = 0; i < path.size(); i++){ int x = path[i]; //cout<<k<<" "<<x<<endl; vector<vector<int>> v_temp(n+1); for(int i = 1; i <= n; i++){ if(i != x){ for(int j = 0; j < (int)v[i].size(); j++){ if(v[i][j] != x){ v_temp[i].push_back(v[i][j]); } } } } res = min(res, result(v_temp, n, k-1)); } return res; } int main(){ int n, m, k; cin>>n>>m>>k; for(int i = 0; i < m; i++){ int x, y; cin>>x>>y; v[x].push_back(y); } if(n == k){ cout<<0<<endl; return 0; } cout<<result(v, n, k)<<endl; } |