#include <iostream> #include <algorithm> #include <vector> using namespace std; struct v { vector<int> L; int deg_in = 0; int depth = -1; int printed_out = 0; int dead = 0; }; void dfs(vector<v> &G, int k) { //cout<<k<<endl; if(G[k].dead) return; for (auto x: G[k].L) { if (G[x].dead) continue; if (G[x].depth == -1) { dfs(G, x); } G[k].depth = max(G[x].depth + 1, G[k].depth); //cout << k << G[k].depth << "<"<< endl; } if (G[k].depth == -1) G[k].depth = 1; } void print_longest_paths(vector<v> &G, int k, vector<int> &Out) { if (G[k].printed_out) return; G[k].printed_out = 1; for (auto x: G[k].L) { if (G[x].depth == G[k].depth - 1) { print_longest_paths(G, x, Out); break; } } Out.push_back(k); } int GetLongest(vector<v> &G, vector<int> &ToKill) { for (auto x: ToKill) { // cout<<":"<<x<<" :)"<<endl; G[x].dead = 1; for (auto son: G[x].L) G[son].deg_in--; } for (auto &x: G) { x.depth = -1; x.printed_out = 0; } int biggest = 0; for(int i = 0;i < G.size();i++) { if (G[i].dead) continue; dfs(G, i); } for (auto x: G) { biggest = max(x.depth, biggest); } //cout << biggest<<endl; for (auto x: ToKill) { G[x].dead = 0; for (auto son: G[x].L) G[son].deg_in++; } //cout << V.back().first << endl; // cout << biggest <<" !! "; return biggest; } int main() { int n, m, k; cin >> n >> m >> k; vector <v> G(n); for (int i = 0;i < m;i++) { int a,b; cin>>a>>b; a--;b--; if(a > b) swap(a, b); G[a].L.push_back(b); G[b].deg_in++; } vector<pair<int, vector<int> > > V; for(int i = 0;i < G.size();i++) { dfs(G, i); } if(k == 0) { int best = 0; for(int i = 0;i < G.size();i++) { best = max(G[i].depth, best); } cout << best << endl; return 0; } vector<pair<int, int> > Firsts; for (int i = 0;i < G.size();i++) { Firsts.push_back({G[i].depth, i}); } sort(Firsts.begin(), Firsts.end()); for (auto x = Firsts.rbegin(); x != Firsts.rend();x++) { int i = x->second; //cout << x->first << endl; if(G[i].deg_in == 0) { vector<int> Out; print_longest_paths(G, i, Out); V.push_back({Out.size(), Out}); } } sort(V.begin(), V.end()); // for (auto x = V.begin(); x != V.end();x++) // cout << x->first << endl; vector<int> CandidatesToDelete; int i = 0; for (auto x = V.rbegin(); x != V.rend();x++) { if (i++ == k) break; for (auto y: x->second) CandidatesToDelete.push_back(y); } // cout<<CandidatesToDelete.size() << endl; // for (auto x: CandidatesToDelete) // cout<<x<<"!"<<endl; //return 0; int best = 9999999; for(int i = 0;i < CandidatesToDelete.size();i++) { vector<int> ToKill = {CandidatesToDelete[i]}; if(k == 1) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int j = i + 1;j < CandidatesToDelete.size();j++) { ToKill.push_back(CandidatesToDelete[j]); if(k == 2) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int l = j + 1;l < CandidatesToDelete.size();l++) { ToKill.push_back(CandidatesToDelete[l]); if(k == 3) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int d = l + 1;d < CandidatesToDelete.size();d++) { ToKill.push_back(CandidatesToDelete[d]); if(k == 4) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } } ToKill.pop_back(); } ToKill.pop_back(); } } cout << best << endl; 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | #include <iostream> #include <algorithm> #include <vector> using namespace std; struct v { vector<int> L; int deg_in = 0; int depth = -1; int printed_out = 0; int dead = 0; }; void dfs(vector<v> &G, int k) { //cout<<k<<endl; if(G[k].dead) return; for (auto x: G[k].L) { if (G[x].dead) continue; if (G[x].depth == -1) { dfs(G, x); } G[k].depth = max(G[x].depth + 1, G[k].depth); //cout << k << G[k].depth << "<"<< endl; } if (G[k].depth == -1) G[k].depth = 1; } void print_longest_paths(vector<v> &G, int k, vector<int> &Out) { if (G[k].printed_out) return; G[k].printed_out = 1; for (auto x: G[k].L) { if (G[x].depth == G[k].depth - 1) { print_longest_paths(G, x, Out); break; } } Out.push_back(k); } int GetLongest(vector<v> &G, vector<int> &ToKill) { for (auto x: ToKill) { // cout<<":"<<x<<" :)"<<endl; G[x].dead = 1; for (auto son: G[x].L) G[son].deg_in--; } for (auto &x: G) { x.depth = -1; x.printed_out = 0; } int biggest = 0; for(int i = 0;i < G.size();i++) { if (G[i].dead) continue; dfs(G, i); } for (auto x: G) { biggest = max(x.depth, biggest); } //cout << biggest<<endl; for (auto x: ToKill) { G[x].dead = 0; for (auto son: G[x].L) G[son].deg_in++; } //cout << V.back().first << endl; // cout << biggest <<" !! "; return biggest; } int main() { int n, m, k; cin >> n >> m >> k; vector <v> G(n); for (int i = 0;i < m;i++) { int a,b; cin>>a>>b; a--;b--; if(a > b) swap(a, b); G[a].L.push_back(b); G[b].deg_in++; } vector<pair<int, vector<int> > > V; for(int i = 0;i < G.size();i++) { dfs(G, i); } if(k == 0) { int best = 0; for(int i = 0;i < G.size();i++) { best = max(G[i].depth, best); } cout << best << endl; return 0; } vector<pair<int, int> > Firsts; for (int i = 0;i < G.size();i++) { Firsts.push_back({G[i].depth, i}); } sort(Firsts.begin(), Firsts.end()); for (auto x = Firsts.rbegin(); x != Firsts.rend();x++) { int i = x->second; //cout << x->first << endl; if(G[i].deg_in == 0) { vector<int> Out; print_longest_paths(G, i, Out); V.push_back({Out.size(), Out}); } } sort(V.begin(), V.end()); // for (auto x = V.begin(); x != V.end();x++) // cout << x->first << endl; vector<int> CandidatesToDelete; int i = 0; for (auto x = V.rbegin(); x != V.rend();x++) { if (i++ == k) break; for (auto y: x->second) CandidatesToDelete.push_back(y); } // cout<<CandidatesToDelete.size() << endl; // for (auto x: CandidatesToDelete) // cout<<x<<"!"<<endl; //return 0; int best = 9999999; for(int i = 0;i < CandidatesToDelete.size();i++) { vector<int> ToKill = {CandidatesToDelete[i]}; if(k == 1) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int j = i + 1;j < CandidatesToDelete.size();j++) { ToKill.push_back(CandidatesToDelete[j]); if(k == 2) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int l = j + 1;l < CandidatesToDelete.size();l++) { ToKill.push_back(CandidatesToDelete[l]); if(k == 3) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } for (int d = l + 1;d < CandidatesToDelete.size();d++) { ToKill.push_back(CandidatesToDelete[d]); if(k == 4) { best = min(best, GetLongest(G, ToKill)); ToKill.pop_back(); continue; } } ToKill.pop_back(); } ToKill.pop_back(); } } cout << best << endl; return 0; } |