#include <bits/stdc++.h> using namespace std; using u64 = uint64_t; using i64 = int64_t; using ci64 = int64_t const; using vi = vector<i64>; using vvi = vector<vector<i64>>; using cvi = vector<i64> const; using cvvi = vector<vector<i64>> const; // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } void print_tpl_helper(ostream&, bool) {} template <typename T, typename ...Ts> void print_tpl_helper(ostream& os, bool first, const T& v, const Ts& ...vs) { if (!first){os << ',';} os << v; print_tpl_helper(os, false, vs...); } template <typename ...Ts> ostream& print_struct(ostream& os, const char* classname, const Ts& ...vs) { // todo: named fields os << classname << '('; print_tpl_helper(os, true, vs...); return os << ')'; } struct Example { char symbol; u64 val; friend ostream& operator<<(ostream& os, const Example& elem) { return print_struct(os, "Op", elem.symbol, elem.val); } }; // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 17) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> T read() { T e; cin >> e; return e; } void read() {} template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); } template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; } template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} // range, len, max {{{ i64 max(i64 a, i64 b) { return a < b ? b : a; } vi range(i64 start, i64 end) { vi v; for (i64 i = start; i < end; i++) v.push_back(i); return v; } vi range(i64 end) { return range(0, end); } template <typename DS> i64 len(DS t) { return (i64)t.size(); } template <typename DS> i64 tmax(DS t) { if (t.empty()) { dprintln("'max' on empty range, returning 0."); return 0; } return *max_element(t.begin(), t.end()); } // }}} i64 n; vector<vector<i64>> parents, children; vector<i64> fav_parents; vector<i64> result; i64 max_node; i64 max_result; i64 res = 1234567; vector<i64> nodes; i64 ops; void calculate_result(i64 da, i64 db, i64 dc, i64 dd) { result.clear(); result.resize(n); fav_parents.clear(); fav_parents.resize(n, -1); max_result = -1; max_node = -1; for (auto b : nodes) { if (b == da || b == db || b == dc || b == dd) { result[b] = -1; continue; } for (auto a : parents[b]) { ++ops; if (result[b] < result[a] + 1) { result[b] = result[a] + 1; fav_parents[b] = a; } } if (max_result < result[b]) { max_result = result[b]; max_node = b; } } res = min(res, max_result); } vi longest_path; void get_longest_path(i64 da, i64 db, i64 dc, i64 dd) { calculate_result(da, db, dc, dd); longest_path.clear(); for (i64 a = max_node; a != -1; a = fav_parents[a]) longest_path.push_back(a); } i64 m, k; void sudo_make_me_a_special_sandwich() { get_longest_path(-1, -1, -1,- 1); if (k < 1) return; vi const node_set0 = longest_path; for (auto a : node_set0) { get_longest_path(a, -1, -1, -1); if (k < 2) continue; vi const node_set1 = longest_path; for (auto b : node_set1) { if (b <= a && find(node_set0.begin(), node_set0.end(), b) != node_set0.end()) continue; get_longest_path(a, b, -1, -1); if (k < 3) continue; vi const node_set2 = longest_path; for (auto c : node_set2) { if (c <= a && find(node_set0.begin(), node_set0.end(), c) != node_set0.end()) continue; if (c <= b && find(node_set1.begin(), node_set1.end(), c) != node_set1.end()) continue; get_longest_path(a, b, c, -1); if (k < 4) continue; for (auto d : longest_path) { if (d <= a && find(node_set0.begin(), node_set0.end(), d) != node_set0.end()) continue; if (d <= b && find(node_set1.begin(), node_set1.end(), d) != node_set1.end()) continue; if (d <= c && find(node_set2.begin(), node_set2.end(), d) != node_set2.end()) continue; calculate_result(a, b, c, d); dprintln(ops); } } } } } void go() { read(n); read(m, k); parents.resize(n); children.resize(n); nodes = range(n); for (auto i : range(m)) { i64 a, b; read(a, b); a--; b--; children[a].push_back(b); parents[b].push_back(a); } sudo_make_me_a_special_sandwich(); println(res + 1); dprintln(ops); } int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } //
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <bits/stdc++.h> using namespace std; using u64 = uint64_t; using i64 = int64_t; using ci64 = int64_t const; using vi = vector<i64>; using vvi = vector<vector<i64>>; using cvi = vector<i64> const; using cvvi = vector<vector<i64>> const; // print tuples {{{ template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& t) { return os << '[' << t.first << ',' << t.second << ']'; } void print_tpl_helper(ostream&, bool) {} template <typename T, typename ...Ts> void print_tpl_helper(ostream& os, bool first, const T& v, const Ts& ...vs) { if (!first){os << ',';} os << v; print_tpl_helper(os, false, vs...); } template <typename ...Ts> ostream& print_struct(ostream& os, const char* classname, const Ts& ...vs) { // todo: named fields os << classname << '('; print_tpl_helper(os, true, vs...); return os << ')'; } struct Example { char symbol; u64 val; friend ostream& operator<<(ostream& os, const Example& elem) { return print_struct(os, "Op", elem.symbol, elem.val); } }; // }}} // print containers {{{ template <typename It> void print(ostream& os, It begin, It end, u64 len, u64 limit = 17) { u64 count = 0; os << "{"; while (begin != end && count < limit) { os << "(" << *begin << ")"; count++; begin++; } if (begin != end) os << "... " << len << " total"; os << "}"; } #define MAKE_PRINTER_1(container) \ template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; } #define MAKE_PRINTER_2(container) \ template <typename T1, typename T2> \ ostream& operator<<(ostream& os, const container<T1, T2>& t) { \ print(os, t.begin(), t.end(), t.size()); \ return os; \ } MAKE_PRINTER_1(vector) MAKE_PRINTER_2(map) MAKE_PRINTER_1(set) MAKE_PRINTER_2(unordered_map) MAKE_PRINTER_1(unordered_set) #undef MAKE_PRINTER_1 #undef MAKE_PRINTER_2 // }}} // read/write {{{ template <typename T> T read() { T e; cin >> e; return e; } void read() {} template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); } template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; } template <typename T> struct identity { const T& operator()(const T& t) const { return t; } }; #define PRINTERS(FNAME, OUTP) \ template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \ void FNAME##ln() { OUTP << '\n'; } \ template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \ template <typename T, typename F = identity<typename T::value_type>> \ void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); } PRINTERS(print, cout) #ifdef DEBUG_PRINTS PRINTERS(dprint, cerr) #else # define dprint(...) # define dprintv(...) # define dprintln(...) #endif /// }}} // range, len, max {{{ i64 max(i64 a, i64 b) { return a < b ? b : a; } vi range(i64 start, i64 end) { vi v; for (i64 i = start; i < end; i++) v.push_back(i); return v; } vi range(i64 end) { return range(0, end); } template <typename DS> i64 len(DS t) { return (i64)t.size(); } template <typename DS> i64 tmax(DS t) { if (t.empty()) { dprintln("'max' on empty range, returning 0."); return 0; } return *max_element(t.begin(), t.end()); } // }}} i64 n; vector<vector<i64>> parents, children; vector<i64> fav_parents; vector<i64> result; i64 max_node; i64 max_result; i64 res = 1234567; vector<i64> nodes; i64 ops; void calculate_result(i64 da, i64 db, i64 dc, i64 dd) { result.clear(); result.resize(n); fav_parents.clear(); fav_parents.resize(n, -1); max_result = -1; max_node = -1; for (auto b : nodes) { if (b == da || b == db || b == dc || b == dd) { result[b] = -1; continue; } for (auto a : parents[b]) { ++ops; if (result[b] < result[a] + 1) { result[b] = result[a] + 1; fav_parents[b] = a; } } if (max_result < result[b]) { max_result = result[b]; max_node = b; } } res = min(res, max_result); } vi longest_path; void get_longest_path(i64 da, i64 db, i64 dc, i64 dd) { calculate_result(da, db, dc, dd); longest_path.clear(); for (i64 a = max_node; a != -1; a = fav_parents[a]) longest_path.push_back(a); } i64 m, k; void sudo_make_me_a_special_sandwich() { get_longest_path(-1, -1, -1,- 1); if (k < 1) return; vi const node_set0 = longest_path; for (auto a : node_set0) { get_longest_path(a, -1, -1, -1); if (k < 2) continue; vi const node_set1 = longest_path; for (auto b : node_set1) { if (b <= a && find(node_set0.begin(), node_set0.end(), b) != node_set0.end()) continue; get_longest_path(a, b, -1, -1); if (k < 3) continue; vi const node_set2 = longest_path; for (auto c : node_set2) { if (c <= a && find(node_set0.begin(), node_set0.end(), c) != node_set0.end()) continue; if (c <= b && find(node_set1.begin(), node_set1.end(), c) != node_set1.end()) continue; get_longest_path(a, b, c, -1); if (k < 4) continue; for (auto d : longest_path) { if (d <= a && find(node_set0.begin(), node_set0.end(), d) != node_set0.end()) continue; if (d <= b && find(node_set1.begin(), node_set1.end(), d) != node_set1.end()) continue; if (d <= c && find(node_set2.begin(), node_set2.end(), d) != node_set2.end()) continue; calculate_result(a, b, c, d); dprintln(ops); } } } } } void go() { read(n); read(m, k); parents.resize(n); children.resize(n); nodes = range(n); for (auto i : range(m)) { i64 a, b; read(a, b); a--; b--; children[a].push_back(b); parents[b].push_back(a); } sudo_make_me_a_special_sandwich(); println(res + 1); dprintln(ops); } int main () { // {{{ ios_base::sync_with_stdio(0); cin.tie(0); go(); } // |