#include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <algorithm> #include <set> #include <time.h> #include <queue> typedef long long int64; using namespace std; struct vertex_t { vector<int> adj_in; vector<int> adj_out; }; const int RAND = 32000; inline int longest_path(vector<vector<int> >& g, vector<int>& cache, vector<int>& bad) { int gmax = 0; for (int i = 0; i < g.size(); ++i) { if (find(bad.begin(), bad.end(), i) != bad.end()) { continue; } int max = 1; for (vector<int>::iterator it = g[i].begin(); it != g[i].end(); it++) { if (find(bad.begin(), bad.end(), *it) != bad.end()) { continue; } int len = cache[*it] + 1; if (len > max) { max = len; } } cache[i] = max; if (max > gmax) { gmax = max; } } return gmax; } int main () { int m, n, k; scanf("%d %d %d", &n, &m, &k); vector<vector<int> > graph(n, vector<int>()); for (int i = 0; i < m; ++i) { int from, to; scanf("%d %d", &from, &to); graph[to - 1].push_back(from - 1); } int min_path = 1000; vector<int> cache(n, 0); vector<int> bad(k, 0); if (k == 1) { for (int i = 0; i < n; ++i) { bad[0] = i; int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } else if (k == 2) { for (int i = 0; i < n; ++i) { bad[0] = i; for (int j = i + 1; j < n; ++j) { bad[1] = j; int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } } else { for (int i = 0; i < RAND; ++i) { for (int j = 0; j < k; ++j) { bad[j] = rand() % n; } int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } printf("%d\n", min_path); 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <algorithm> #include <set> #include <time.h> #include <queue> typedef long long int64; using namespace std; struct vertex_t { vector<int> adj_in; vector<int> adj_out; }; const int RAND = 32000; inline int longest_path(vector<vector<int> >& g, vector<int>& cache, vector<int>& bad) { int gmax = 0; for (int i = 0; i < g.size(); ++i) { if (find(bad.begin(), bad.end(), i) != bad.end()) { continue; } int max = 1; for (vector<int>::iterator it = g[i].begin(); it != g[i].end(); it++) { if (find(bad.begin(), bad.end(), *it) != bad.end()) { continue; } int len = cache[*it] + 1; if (len > max) { max = len; } } cache[i] = max; if (max > gmax) { gmax = max; } } return gmax; } int main () { int m, n, k; scanf("%d %d %d", &n, &m, &k); vector<vector<int> > graph(n, vector<int>()); for (int i = 0; i < m; ++i) { int from, to; scanf("%d %d", &from, &to); graph[to - 1].push_back(from - 1); } int min_path = 1000; vector<int> cache(n, 0); vector<int> bad(k, 0); if (k == 1) { for (int i = 0; i < n; ++i) { bad[0] = i; int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } else if (k == 2) { for (int i = 0; i < n; ++i) { bad[0] = i; for (int j = i + 1; j < n; ++j) { bad[1] = j; int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } } else { for (int i = 0; i < RAND; ++i) { for (int j = 0; j < k; ++j) { bad[j] = rand() % n; } int res = longest_path(graph, cache, bad); if (res < min_path) { min_path = res; } } } printf("%d\n", min_path); return 0; } |