結果
問題 | No.898 tri-βutree |
ユーザー |
![]() |
提出日時 | 2019-10-04 21:32:01 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 794 ms / 4,000 ms |
コード長 | 7,230 bytes |
コンパイル時間 | 2,513 ms |
コンパイル使用メモリ | 207,960 KB |
最終ジャッジ日時 | 2025-01-07 20:24:09 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 |
ソースコード
#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 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...);}#ifndef STOPIT#define debug(...)\std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\debug_impl(__VA_ARGS__);\std::cerr << std::noboolalpha;#else#define debug 0;#endiftemplate< typename F >class fixed_point : F {public:explicit constexpr fixed_point (F&& f) noexcept: F(std::forward< F >(f)) {}template< typename ... Args >constexpr decltype(auto) operator()(Args&& ... args) const{ return F::operator()(*this, std::forward< Args >(args)...); }};template< typename F >static inline constexpr decltype(auto) fix (F&& f) noexcept{ return fixed_point< F >{std::forward< F >(f)}; }// Class for calculating LCA.// Dependent libraries: Combinator.template <typename T>class lowest_common_ancestor {struct edge {size_t to; T cost;edge(size_t to, T cost) : to(to), cost(cost){}};size_t n, lg;std::vector<std::vector<edge>> graph;std::vector<unsigned> depth;std::vector<T> weighted_depth;std::vector<std::vector<size_t>> prt;public:using cost_type = T;lowest_common_ancestor(size_t n) :n(n), lg(std::log2(n)),graph(n), depth(n, 0), weighted_depth(n, 0),prt(lg + 1, std::vector<size_t>(n)) {}// Insert an edge.void insert (size_t u, size_t v, T cost = 1) {graph.at(u).emplace_back(v, cost);graph.at(v).emplace_back(u, cost);}// Build a doubling table.void build (size_t root = 0) {fix ([&](auto dfs, size_t crr, size_t p) -> void {prt.at(0).at(crr) = p;for (auto const& e : graph.at(crr)) {if (e.to == p) continue;depth.at(e.to) = depth.at(crr) + 1;weighted_depth.at(e.to) = weighted_depth.at(crr) + e.cost;dfs(e.to, crr);}})(root, root);for (size_t p = 1; p <= lg; p++) {for (size_t i = 0; i < n; i++) {prt.at(p).at(i) = prt.at(p - 1).at(prt.at(p - 1).at(i));}}}// Calculate the lca.auto operator()(size_t u, size_t v) const -> size_t {if (depth.at(u) < depth.at(v)) std::swap(u, v);auto diff = depth.at(u) - depth.at(v);if (diff > 0) {for (size_t p = lg, q = std::pow(2, lg); p <= lg; p--, q /= 2) {if (diff > q) u = prt.at(p).at(u), diff -= q;}u = prt.at(0).at(u), diff--;}assert(diff == 0), assert(depth.at(u) == depth.at(v));if (u == v) return u;for (size_t p = lg, q = std::pow(2, lg); p <= lg; p--, q /= 2) {auto next_u = prt.at(p).at(u);auto next_v = prt.at(p).at(v);if (next_u != next_v) u = next_u, v = next_v;}assert(u != v);u = prt.at(0).at(u), v = prt.at(0).at(v), assert(u == v);return u;}// Calculate the count-based distance.auto row_dist (size_t u, size_t v) const -> unsigned {return depth.at(u) + depth.at(v) - 2 * depth.at(operator()(u, v));}// Calculate the count-based distance.auto weighted_dist (size_t u, size_t v) const -> T {auto& x = weighted_depth;return x.at(u) + x.at(v) - 2 * x.at(operator()(u, v));}};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...>()); }int main() {std::cin.tie(0); std::cin.sync_with_stdio(false);int n; std::cin >> n;auto lca = lowest_common_ancestor< lint >(n);loop(n - 1) {int u, v, w;std::cin >> u >> v >> w;lca.insert(u, v, w);}lca.build();int q; std::cin >> q;loop(q) {int x, y, z;std::cin >> x >> y >> z;auto ans =lca.weighted_dist(x, y)+ lca.weighted_dist(y, z)+ lca.weighted_dist(z, x);ans /= 2;std::cout << ans << std::endl;}return 0;}