結果

問題 No.898 tri-βutree
ユーザー ngtkanangtkana
提出日時 2019-10-04 21:32:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 606 ms / 4,000 ms
コード長 7,230 bytes
コンパイル時間 3,460 ms
コンパイル使用メモリ 211,100 KB
実行使用メモリ 32,556 KB
最終ジャッジ日時 2023-08-08 15:49:43
合計ジャッジ時間 14,552 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 241 ms
32,556 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 566 ms
25,276 KB
testcase_08 AC 543 ms
25,160 KB
testcase_09 AC 551 ms
25,416 KB
testcase_10 AC 552 ms
25,220 KB
testcase_11 AC 554 ms
25,276 KB
testcase_12 AC 606 ms
25,168 KB
testcase_13 AC 571 ms
25,296 KB
testcase_14 AC 540 ms
25,280 KB
testcase_15 AC 546 ms
25,260 KB
testcase_16 AC 548 ms
25,296 KB
testcase_17 AC 583 ms
25,464 KB
testcase_18 AC 555 ms
25,260 KB
testcase_19 AC 544 ms
25,260 KB
testcase_20 AC 547 ms
25,220 KB
testcase_21 AC 554 ms
25,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
#endif

template< 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;
}
0