結果

問題 No.898 tri-βutree
ユーザー tkmst201
提出日時 2021-02-15 16:53:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 213 ms / 4,000 ms
コード長 8,500 bytes
コンパイル時間 2,645 ms
コンパイル使用メモリ 221,116 KB
最終ジャッジ日時 2025-01-18 21:11:04
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:212:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  212 |                 scanf("%d %d %d", &u, &v, &w);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:234:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  234 |                 scanf("%d %d %d", &x, &y, &z);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

#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() {
using HLD = HeavyLightDecomposition;
int N;
cin >> N;
HLD::Graph hlg(N);
vector<vector<pii>> g(N);
REP(i, N - 1) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
hlg[u].emplace_back(v);
hlg[v].emplace_back(u);
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
HLD hld(hlg, true);
vector<ll> dist(N);
auto dfs = [&](auto self, int u, int p) -> void {
for (auto [v, d] : g[u]) {
if (v == p) continue;
dist[v] = dist[u] + d;
self(self, v, u);
}
};
dfs(dfs, 0, -1);
int Q;
cin >> Q;
while (Q--) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
const int xy = hld.get_lca(x, y);
auto func = [&](int i, int j) -> ll {
return dist[i] + dist[j] - 2 * dist[hld.get_lca(i, j)];
};
ll ans = (func(x, y) + func(y, z) + func(x, z)) / 2;
printf("%lld\n", ans);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0