結果
問題 | No.898 tri-βutree |
ユーザー | tkmst201 |
提出日時 | 2021-02-13 20:49:30 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 8,749 bytes |
コンパイル時間 | 3,431 ms |
コンパイル使用メモリ | 231,324 KB |
実行使用メモリ | 44,968 KB |
最終ジャッジ日時 | 2024-07-21 00:40:09 |
合計ジャッジ時間 | 9,492 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 106 ms
44,968 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// struct HeavyLightDecomposition { using size_type = std::uint_fast32_t; using Graph = std::vector<std::vector<size_type>>; private: size_type bf_n; // グラフの頂点数 std::vector<size_type> par_; // [v] := 頂点 v の親の頂点番号(存在しなければ自分自身) std::vector<size_type> sub_size_; // [v] := 頂点 v を根とする部分木のサイズ std::vector<size_type> depth_; // [v] := 頂点 v の元のグラフでの深さ std::vector<size_type> tree_id_; // [v] := 頂点 v が属する木の id std::vector<size_type> roots_; // [i] := i 番目の木の root std::vector<size_type> heavy_map_; // [v] := 頂点 v が属する heavy-path id std::vector<size_type> head_; // [i] := heavy-path i の最も根に近い頂点番号 std::vector<size_type> heavy_size_ ; // [i] := heavy-path i に属する頂点の個数 std::vector<size_type> heavy_depth_; // [i] := heavy-path i から根までに通る light-edge の個数 // euler-tour std::vector<size_type> in_; // [v] := 頂点 v の EulerTour 順序(同一 heavy-path 内では連続) std::vector<size_type> out_; // [v] := 頂点 v から出るときの EulerTour 順序 std::vector<size_type> euler_map_; // [i] := EulerTour 順序が i であるような頂点 // heavy-path doubling std::vector<std::vector<size_type>> par_dblng_; // [k][i] := heavy-path i から 2^k 回 light-edge を上った先の頂点 public: HeavyLightDecomposition(const Graph & g, bool use_lca = false) : HeavyLightDecomposition(g, g.size(), use_lca) {} HeavyLightDecomposition(const Graph & g, size_type root, bool use_lca) : bf_n(g.size()) { par_.resize(bf_size()); sub_size_.resize(bf_size()); depth_.resize(bf_size()); tree_id_.assign(bf_size(), bf_size()); std::vector<size_type> next(bf_size()); // [v] := 頂点 v と同一 heavy-path 内で v より 1 つ葉側の頂点(存在しなければ自分自身) for (size_type i = 0; i < bf_size(); ++i) { if (tree_id_[i] != bf_size()) continue; if (root != bf_size() && i != root) continue; std::stack<std::pair<size_type, size_type>> stk; par_[i] = i; depth_[i] = 0; tree_id_[i] = roots_.size(); stk.emplace(i, 0); while (!stk.empty()) { const size_type u = stk.top().first, i = stk.top().second; stk.pop(); if (i < g[u].size()) { stk.emplace(u, i + 1); const size_type v = g[u][i]; if (v == par_[u]) continue; par_[v] = u; depth_[v] = depth_[u] + 1; tree_id_[v] = roots_.size(); stk.emplace(v, 0); } else { size_type mx = 0; next[u] = u; sub_size_[u] = 1; for (size_type v : g[u]) { if (v == par_[u]) continue; sub_size_[u] += sub_size_[v]; if (mx < sub_size_[v]) { mx = sub_size_[v]; next[u] = v; } } } } roots_.emplace_back(i); } heavy_map_.resize(bf_size()); in_.resize(bf_size()); out_.resize(bf_size()); euler_map_.reserve(bf_size()); for (size_type root : roots_) { std::stack<std::pair<size_type, size_type>> stk; heavy_map_[root] = head_.size(); head_.emplace_back(root); heavy_size_.emplace_back(1); heavy_depth_.emplace_back(0); stk.emplace(root, 0); while (!stk.empty()) { const size_type u = stk.top().first, i = stk.top().second; stk.pop(); if (i < g[u].size()) { stk.emplace(u, i + 1); const size_type v = g[u][i]; if (v != par_[u] && v != next[u]) { heavy_map_[v] = head_.size(); head_.emplace_back(v); heavy_size_.emplace_back(1); heavy_depth_.emplace_back(heavy_depth_[heavy_map_[u]] + 1); stk.emplace(v, 0); } } if (i == 0) { in_[u] = euler_map_.size(); euler_map_.emplace_back(u); const size_type v = next[u]; if (v != u) { heavy_map_[v] = heavy_map_[u]; ++heavy_size_[heavy_map_[u]]; stk.emplace(v, 0); } } if (i == g[u].size()) out_[u] = euler_map_.size(); } } if (!use_lca) return; size_type max_depth = *std::max_element(begin(heavy_depth_), end(heavy_depth_)); size_type lglg_n = 0; while ((1 << lglg_n) < max_depth) ++lglg_n; par_dblng_.assign(lglg_n + 1, std::vector<size_type>(af_size())); for (size_type i = 0; i < af_size(); ++i) par_dblng_[0][i] = par_[head_[i]]; for (size_type i = 0; i < lglg_n; ++i) { for (size_type j = 0; j < af_size(); ++j) { par_dblng_[i + 1][j] = par_dblng_[i][heavy_map_[par_dblng_[i][j]]]; } } } size_type bf_size() const noexcept { return bf_n; } size_type af_size() const noexcept { return head_.size(); } size_type par(size_type v) const { assert(v < bf_size()); return par_[v]; } size_type sub_size(size_type v) const { assert(v < bf_size()); return sub_size_[v]; } size_type depth(size_type v) const { assert(v < bf_size()); return depth_[v]; } size_type tree_id(size_type v) const { assert(v < bf_size()); return tree_id_[v]; } size_type tree_cnt() const noexcept { return roots_.size(); } const std::vector<size_type> & trees() const noexcept { return roots_; } size_type heavy_map(size_type v) const { assert(v < bf_size()); return heavy_map_[v]; } size_type head(size_type k) const { assert(k < af_size()); return head_[k]; } size_type heavy_size(size_type k) const { assert(k < af_size()); return heavy_size_[k]; } size_type heavy_depth(size_type k) const { assert(k < af_size()); return heavy_depth_[k]; } size_type in(size_type v) const { assert(v < bf_size()); return in_[v]; } size_type out(size_type v) const { assert(v < bf_size()); return out_[v]; } size_type euler_map(size_type k) const { assert(k < bf_size()); return euler_map_[k]; } const std::vector<std::vector<size_type>> & par_dblng() const { assert(!par_dblng_.empty()); return par_dblng_; } std::pair<size_type, size_type> get_lca_path(size_type x, size_type y) const { assert(!par_dblng_.empty()); assert(x < bf_size()); assert(y < bf_size()); assert(tree_id_[x] == tree_id_[y]); if (heavy_map_[x] == heavy_map_[y]) return {x, y}; bool isswap = heavy_depth_[heavy_map_[x]] < heavy_depth_[heavy_map_[y]]; if (isswap) std::swap(x, y); const size_type diff = heavy_depth_[heavy_map_[x]] - heavy_depth_[heavy_map_[y]]; for (size_type i = par_dblng_.size(); i > 0; --i) { if (diff >> (i - 1) & 1) x = par_dblng_[i - 1][heavy_map_[x]]; } if (heavy_map_[x] == heavy_map_[y]) return isswap ? std::make_pair(y, x) : std::make_pair(x, y); for (size_type i = par_dblng_.size(); i > 0; --i) { const size_type p1 = par_dblng_[i - 1][heavy_map_[x]], p2 = par_dblng_[i - 1][heavy_map_[y]]; if (heavy_map_[p1] != heavy_map_[p2]) x = p1, y = p2; } x = par_dblng_[0][heavy_map_[x]]; y = par_dblng_[0][heavy_map_[y]]; return isswap ? std::make_pair(y, x) : std::make_pair(x, y); } size_type get_lca(size_type x, size_type y) { assert(!par_dblng_.empty()); assert(x < bf_size()); assert(y < bf_size()); std::pair<size_type, size_type> res = get_lca_path(x, y); return in_[res.first] < in_[res.second] ? res.first : res.second; } }; int main() { int N; cin >> N; vector<vector<pii>> g(N); using HLD = HeavyLightDecomposition; HLD::Graph hlg(N); REP(i, N - 1) { int u, v, w; scanf("%d %d %d", &u, &v, &w); g[u].emplace_back(v, w); g[v].emplace_back(u, w); hlg[u].emplace_back(v); hlg[v].emplace_back(u); } int Q; cin >> Q; vector<vector<int>> ev(N); REP(i, Q) { int x, y, z; scanf("%d %d %d", &x, &y, &z); ev[x].emplace_back(i); ev[y].emplace_back(i); ev[z].emplace_back(i); } HLD hld(hlg, true); vector<ll> ans(Q), dist(N); vector<int> last(Q, -1); auto dfs = [&](auto self, int u, int p) -> void { for (int q : ev[u]) { if (last[q] == -1) last[q] = u; else if (last[q] < N) { const int v = last[q]; ans[q] += dist[u] + dist[v] - dist[hld.get_lca(u, v)]; last[q] = N + u; } else { const int v = last[q] - N; ans[q] += dist[u] - dist[hld.get_lca(u, v)]; } } for (auto [v, c] : g[u]) { if (v == p) continue; dist[v] = dist[u] + c; self(self, v, u); } }; dfs(dfs, 0, -1); REP(i, Q) printf("%lld\n", ans[i]); }