#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<int> > edges; vector<int> blacklist; vector<int> vis; void dfs(int node) { if (blacklist[node]) { vis[node] = 0; return; } if (vis[node]!=-1)return; int res = 1; for (int c:edges[node]) { dfs(c); res = max(res, vis[c]+1); } vis[node] = res; } int main() { int n,m,k; cin >> n >> m >> k; edges.resize(n); blacklist.resize(n); for (int i=0; i < k; i++) blacklist[i] = 1; vis.resize(n); for (int i=0; i < m; i++) { int a,b; cin >> a >> b; edges[a-1].push_back(b-1); } int res = 1e9; int iter = 30*1000*1000 / (n+m); //cerr << iter << endl; for (int i=0; i < iter; i ++) { random_shuffle(blacklist.begin(), blacklist.end()); for (int&v:vis)v=-1; for (int j=0; j < n; j++) dfs(j); int nres = *max_element(vis.begin(), vis.end()); res = min(res, nres); } cout << res << 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<int> > edges; vector<int> blacklist; vector<int> vis; void dfs(int node) { if (blacklist[node]) { vis[node] = 0; return; } if (vis[node]!=-1)return; int res = 1; for (int c:edges[node]) { dfs(c); res = max(res, vis[c]+1); } vis[node] = res; } int main() { int n,m,k; cin >> n >> m >> k; edges.resize(n); blacklist.resize(n); for (int i=0; i < k; i++) blacklist[i] = 1; vis.resize(n); for (int i=0; i < m; i++) { int a,b; cin >> a >> b; edges[a-1].push_back(b-1); } int res = 1e9; int iter = 30*1000*1000 / (n+m); //cerr << iter << endl; for (int i=0; i < iter; i ++) { random_shuffle(blacklist.begin(), blacklist.end()); for (int&v:vis)v=-1; for (int j=0; j < n; j++) dfs(j); int nres = *max_element(vis.begin(), vis.end()); res = min(res, nres); } cout << res << endl; } |