#include<bits/stdc++.h> using namespace std; queue<int> q; int n,m,k; vector<int> G[301]; pair<int, int> krawedzie[401]; void printG() { cout<<"printing G: "<<endl; for(int i=1;i<=n;i++) { cout<<i<<":"<<endl; for(int j=0;j<G[i].size();j++) { cout<<G[i][j]<<" "; } cout<<endl; } } int rekur(int k, set<int> removed) { vector<int> starting; int wejscia[301]; for(int i=0;i<m;i++) { if(removed.find(krawedzie[i].first)==removed.end()) wejscia[krawedzie[i].second]++; } int max_length=0; set<int> set_to_test; int path_length[301]; set<int> paths[301]; for(int i=0;i<=n;i++) { path_length[i]=1; paths[i].clear(); wejscia[i]=0; starting.clear(); } for(int i=1;i<=n;i++) { if(wejscia[i]==0 and removed.find(i)==removed.end()) { starting.push_back(i); path_length[i]=1; } } set_to_test.clear(); max_length=1; for(int i=0;i<starting.size();i++) { paths[starting[i]].insert(starting[i]); q.push(starting[i]); } while(!q.empty()) { int actual = q.front(); //printG(); q.pop(); for(int i=0;i<G[actual].size();i++) { if(removed.find(G[actual][i])!=removed.end()) continue; int sizeOfChild = paths[G[actual][i]].size(); int mysize = paths[actual].size(); //cout<<actual<<" "<<G[actual][i]<<endl; if(mysize+1>=sizeOfChild) { paths[G[actual][i]].insert(paths[actual].begin(), paths[actual].end()); paths[G[actual][i]].insert(G[actual][i]); q.push(G[actual][i]); path_length[G[actual][i]]=path_length[actual]+1; max_length = max(path_length[G[actual][i]],max_length); } } } if(k==0) return max_length; for(int i=1;i<=n;i++) { if(path_length[i]==max_length) set_to_test.insert(paths[i].begin(),paths[i].end()); } //usuwanie elementóœ int best = 500; int result = -1; set<int>::iterator it; for (it=set_to_test.begin(); it!=set_to_test.end(); ++it) { removed.insert(*it); result = rekur(k-1,removed); //cout<<"removed "<<*it<<" result: "<<result<<endl; best=min(best, result); removed.erase(*it); } //cout<<k<<" "<<best<<endl; return best; } int main() { int a,b; cin>>n>>m>>k; if(k>=n) { cout<<0<<endl; return 0; } for(int i=0;i<m;i++) { cin>>a>>b; krawedzie[i] = make_pair(a, b); //cout<<"pushing "<<b<<" to "<<a<<endl; G[a].push_back(b); } set<int>xd; //printG(); cout<<rekur(k,xd); 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 | #include<bits/stdc++.h> using namespace std; queue<int> q; int n,m,k; vector<int> G[301]; pair<int, int> krawedzie[401]; void printG() { cout<<"printing G: "<<endl; for(int i=1;i<=n;i++) { cout<<i<<":"<<endl; for(int j=0;j<G[i].size();j++) { cout<<G[i][j]<<" "; } cout<<endl; } } int rekur(int k, set<int> removed) { vector<int> starting; int wejscia[301]; for(int i=0;i<m;i++) { if(removed.find(krawedzie[i].first)==removed.end()) wejscia[krawedzie[i].second]++; } int max_length=0; set<int> set_to_test; int path_length[301]; set<int> paths[301]; for(int i=0;i<=n;i++) { path_length[i]=1; paths[i].clear(); wejscia[i]=0; starting.clear(); } for(int i=1;i<=n;i++) { if(wejscia[i]==0 and removed.find(i)==removed.end()) { starting.push_back(i); path_length[i]=1; } } set_to_test.clear(); max_length=1; for(int i=0;i<starting.size();i++) { paths[starting[i]].insert(starting[i]); q.push(starting[i]); } while(!q.empty()) { int actual = q.front(); //printG(); q.pop(); for(int i=0;i<G[actual].size();i++) { if(removed.find(G[actual][i])!=removed.end()) continue; int sizeOfChild = paths[G[actual][i]].size(); int mysize = paths[actual].size(); //cout<<actual<<" "<<G[actual][i]<<endl; if(mysize+1>=sizeOfChild) { paths[G[actual][i]].insert(paths[actual].begin(), paths[actual].end()); paths[G[actual][i]].insert(G[actual][i]); q.push(G[actual][i]); path_length[G[actual][i]]=path_length[actual]+1; max_length = max(path_length[G[actual][i]],max_length); } } } if(k==0) return max_length; for(int i=1;i<=n;i++) { if(path_length[i]==max_length) set_to_test.insert(paths[i].begin(),paths[i].end()); } //usuwanie elementóœ int best = 500; int result = -1; set<int>::iterator it; for (it=set_to_test.begin(); it!=set_to_test.end(); ++it) { removed.insert(*it); result = rekur(k-1,removed); //cout<<"removed "<<*it<<" result: "<<result<<endl; best=min(best, result); removed.erase(*it); } //cout<<k<<" "<<best<<endl; return best; } int main() { int a,b; cin>>n>>m>>k; if(k>=n) { cout<<0<<endl; return 0; } for(int i=0;i<m;i++) { cin>>a>>b; krawedzie[i] = make_pair(a, b); //cout<<"pushing "<<b<<" to "<<a<<endl; G[a].push_back(b); } set<int>xd; //printG(); cout<<rekur(k,xd); return 0; } |