結果
問題 | No.922 東北きりきざむたん |
ユーザー | ngtkana |
提出日時 | 2019-11-08 22:54:31 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 9,982 bytes |
コンパイル時間 | 3,893 ms |
コンパイル使用メモリ | 238,348 KB |
実行使用メモリ | 27,136 KB |
最終ジャッジ日時 | 2024-09-15 02:03:08 |
合計ジャッジ時間 | 10,700 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,624 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 114 ms
13,056 KB |
testcase_10 | TLE | - |
testcase_11 | AC | 486 ms
10,880 KB |
testcase_12 | AC | 42 ms
14,848 KB |
testcase_13 | AC | 19 ms
6,656 KB |
testcase_14 | AC | 114 ms
19,328 KB |
testcase_15 | AC | 36 ms
15,104 KB |
testcase_16 | TLE | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
ソースコード
#define STOPIT #include <bits/stdc++.h> #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt) using lint = long long; auto mt = std::mt19937_64(std::random_device{}()); auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #ifdef STOPIT #define debug(...) 0 #else #define debug(...)\ do {\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha;\ } while (false) #endif template <typename T> auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);} template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr> auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));} template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr> auto make_vector(size_t sz) {return std::vector<T>(sz);} template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr> auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));} template <typename T, typename Size_t> auto& at(T& t, Size_t i) {return t.at(i);} template <typename T, typename Size_t, typename... Args> auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);} template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr> std::istream& operator>> (std::istream& is, Container& v) { for (auto & x : v) { is >> x; } return is; } template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr > std::ostream& operator<< (std::ostream& os, Container const& v) { os << "{"; for (auto it = v.begin(); it != v.end(); it++) {os << (it != v.begin() ? "," : "") << *it;} return os << "}"; } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>) { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple) { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>) { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple) { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); } class union_find { int n; std::vector<int> prt; public: union_find (int n) : n(n), prt(n, -1){} bool is_root (int x) {return prt.at(x) < 0;} int size (int x) {return -prt.at(find(x));} bool same(int x, int y) {return find(x) == find(y);} int find (int x) { return is_root(x) ? x : prt.at(x) = find(prt.at(x)); } // Returns `true` if x and y are newly connected. // The smaller one x becomes a child of the larger one y. bool unite (int x, int y) { if ((x = find(x)) == (y = find(y))) return false; if (size(x) > size(y)) std::swap(x, y); prt.at(y) += prt.at(x); prt.at(x) = y; return true; } }; template < class Value > struct vending_machine{ Value i; vending_machine(Value i) : i(i){} auto issue() { return i++; } auto peek() const { return i; } }; template < class Value > auto make_vending_machine(Value i) { return vending_machine< Value >(i); } template < class Value > class lowest_common_ancestor { struct edge { int to; Value cost; edge(int to, Value cost) : to(to), cost(cost){} }; int n, lg, powlg; std::vector< std::vector< edge > > graph; std::vector< int > depth; std::vector< Value > w_depth; std::vector< std::vector< int > > table; std::vector< int > & tail; public: using cost_type = Value; lowest_common_ancestor(int n) : n(n), lg(std::log2(n)), powlg(std::pow(2, lg)), graph(n), depth(n, 0), w_depth(n, 0), table(lg + 1, std::vector< int >(n)), tail(table.back()) {} void insert (int u, int v, Value cost = 1) { graph.at(u).emplace_back(v, cost); graph.at(v).emplace_back(u, cost); } void build (int root = 0) { auto dfs = [&](auto f, int crr, int p) -> void { tail.at(crr) = p; for (auto const& e : graph.at(crr)) { if (e.to == p) continue; depth.at(e.to) = depth.at(crr) + 1; w_depth.at(e.to) = w_depth.at(crr) + e.cost; f(f, e.to, crr); } }; dfs(dfs, root, root); for (int p = lg; p >= 1; p--) { auto & crr = table.at(p); auto & nxt = table.at(p - 1); for (int i = 0; i < n; i++) { nxt.at(i) = crr.at(crr.at(i)); } } } auto query(int u, int v) const -> int { if (depth.at(u) < depth.at(v)) std::swap(u, v); auto diff = depth.at(u) - depth.at(v); if (diff > 0) { int coeff = powlg; for (auto const & row : table) { if (coeff < diff) { u = row.at(u); diff -= coeff; } coeff /= 2; } u = tail.at(u); diff--; } assert(diff == 0); assert(depth.at(u) == depth.at(v)); if (u == v) return u; for (auto const & row : table) { auto next_u = row.at(u); auto next_v = row.at(v); if (next_u != next_v) { u = next_u; v = next_v; } } assert(u != v); u = tail.at(u), v = tail.at(v), assert(u == v); return u; } auto row_distance (int u, int v) const -> int { return depth.at(u) + depth.at(v) - 2 * depth.at(query(u, v)); } auto weighted_distance (int u, int v) const -> Value { return w_depth.at(u) + w_depth.at(v) - 2 * w_depth.at(query(u, v)); } }; int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n, m, q; std::cin >> n >> m >> q; auto graph = make_vector< 2, int >(n, 0); auto qf = union_find(n); loop(m) { int u, v; std::cin >> u >> v; u--, v--; graph.at(u).emplace_back(v); graph.at(v).emplace_back(u); qf.unite(u, v); } vending_machine< int > vm{0}; std::vector< int > cmp(n); rep(i, 0, n) { if (qf.is_root(i)) { cmp.at(i) = vm.issue(); } } rep(i, 0, n) { cmp.at(i) = cmp.at(qf.find(i)); } int sz = vm.peek(); std::vector< vending_machine< int > > id_seq(sz, {0}); std::vector< int > id(n); rep(i, 0, n) { id.at(i) = id_seq.at(cmp.at(i)).issue(); } auto internal = make_vector< 2, std::pair< int, int > >(sz, 0); auto external = make_vector< 2, int >(sz, 0); loop(q) { int u, v; std::cin >> u >> v; u--, v--; int x = cmp.at(u), y = cmp.at(v); int i = id.at(u), j = id.at(v); if (x == y) { internal.at(x).emplace_back(i, j); } else { external.at(x).emplace_back(i); external.at(y).emplace_back(j); } } auto sgraph = make_vector< 3, int >(sz, 0, 0); rep(i, 0, sz) sgraph.at(i).resize(id_seq.at(i).peek()); rep(i, 0, n) { for (auto j : graph.at(i)) { int x = cmp.at(i), y = cmp.at(j); assert(x == y); at(sgraph, x, id.at(i)).emplace_back(id.at(j)); } } auto cal = [&] (auto&& internal, auto&& external, auto&& graph, auto n) -> long long { debug(n); // debug(internal); // debug(external); // debug(graph); auto lca = lowest_common_ancestor< int >(n); rep(i, 0, n) { for (auto j : graph.at(i)) { lca.insert(i, j); } } lca.build(); lint ans = 0; for (auto pair : internal) { int u, v; std::tie(u, v) = pair; lint tmp = lca.row_distance(u, v); ans += tmp; } std::vector< lint > sz(n, 0), dp(n); for (auto i : external) sz.at(i)++; auto dfs = [&] (auto&& dfs, auto crr, auto prv) -> void { for (auto nxt : graph.at(crr)) { if (nxt == prv) continue; dfs(dfs, nxt, crr); sz.at(crr) += sz.at(nxt); dp.at(crr) += dp.at(nxt) + sz.at(nxt); } }; dfs(dfs, 0, 0); debug(sz); debug(dp); auto efs = [&](auto&& efs, auto crr, auto prv) -> void { for (auto nxt : graph.at(crr)) { if (nxt == prv) continue; dp.at(nxt) = dp.at(crr) + external.size() - 2 * sz.at(nxt); efs(efs, nxt, crr); } }; efs(efs, 0, 0); debug(dp); ans += *std::min_element(all(dp)); return ans; }; long long ans = 0; rep(i, 0, sz) { ans += cal(internal.at(i), external.at(i), sgraph.at(i), id_seq.at(i).peek()); } std::cout << ans << std::endl; return 0; }