#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; struct Graph { vector<vector<int>> adj_list; Graph(int n) : adj_list(n) { } void add_edge(int v1, int v2) { adj_list[v1].push_back(v2); } int longest_path(vector<int> ignored); }; bool contains(vector<int> &vec, int i) { for (int x : vec) { if (x == i) return true; } return false; } int Graph::longest_path(vector<int> ignored) { vector<int> distance(adj_list.size(), 1); int mx = 0; for (int i = 0; i < adj_list.size(); ++i) { if (contains(ignored, i)) continue; for (int next : adj_list[i]) { if (contains(ignored, next)) continue; distance[next] = max(distance[next], distance[i] + 1); mx = max(mx, distance[next]); } } //for (int i : distance) // cerr << i << " "; //cerr << endl; return mx; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, x, y; cin >> n >> m >> k; Graph g(n); for (int i = 0; i < m; ++i) { cin >> x >> y; g.add_edge(x - 1, y - 1); } if (k == n) cout << 0 << endl; else if (k == 0) cout << g.longest_path({}) << endl; else if (k == 1) { int mn = 1000000; for (int i = 0; i < n; ++i) { mn = min(mn, g.longest_path({i})); } cout << mn << endl; } else if (k == 2) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { mn = min(mn, g.longest_path({i, j})); } } cout << mn << endl; } else if (k == 3) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { mn = min(mn, g.longest_path({i, j, k})); } } } cout << mn << endl; } else if (k >= 4) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { for (int l = k + 1; l < n; ++l) { mn = min(mn, g.longest_path({i, j, k, l})); } } } } cout << mn << 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 | #include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; struct Graph { vector<vector<int>> adj_list; Graph(int n) : adj_list(n) { } void add_edge(int v1, int v2) { adj_list[v1].push_back(v2); } int longest_path(vector<int> ignored); }; bool contains(vector<int> &vec, int i) { for (int x : vec) { if (x == i) return true; } return false; } int Graph::longest_path(vector<int> ignored) { vector<int> distance(adj_list.size(), 1); int mx = 0; for (int i = 0; i < adj_list.size(); ++i) { if (contains(ignored, i)) continue; for (int next : adj_list[i]) { if (contains(ignored, next)) continue; distance[next] = max(distance[next], distance[i] + 1); mx = max(mx, distance[next]); } } //for (int i : distance) // cerr << i << " "; //cerr << endl; return mx; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, x, y; cin >> n >> m >> k; Graph g(n); for (int i = 0; i < m; ++i) { cin >> x >> y; g.add_edge(x - 1, y - 1); } if (k == n) cout << 0 << endl; else if (k == 0) cout << g.longest_path({}) << endl; else if (k == 1) { int mn = 1000000; for (int i = 0; i < n; ++i) { mn = min(mn, g.longest_path({i})); } cout << mn << endl; } else if (k == 2) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { mn = min(mn, g.longest_path({i, j})); } } cout << mn << endl; } else if (k == 3) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { mn = min(mn, g.longest_path({i, j, k})); } } } cout << mn << endl; } else if (k >= 4) { int mn = 1000000; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { for (int l = k + 1; l < n; ++l) { mn = min(mn, g.longest_path({i, j, k, l})); } } } } cout << mn << endl; } return 0; } |