結果

問題 No.898 tri-βutree
ユーザー tkmst201tkmst201
提出日時 2021-02-15 16:47:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 4,000 ms
コード長 9,078 bytes
コンパイル時間 3,301 ms
コンパイル使用メモリ 222,656 KB
実行使用メモリ 26,452 KB
最終ジャッジ日時 2024-04-26 09:32:36
合計ジャッジ時間 7,656 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
26,092 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 180 ms
25,944 KB
testcase_08 AC 181 ms
25,956 KB
testcase_09 AC 181 ms
26,328 KB
testcase_10 AC 186 ms
26,068 KB
testcase_11 AC 184 ms
26,452 KB
testcase_12 AC 181 ms
26,060 KB
testcase_13 AC 173 ms
25,944 KB
testcase_14 AC 189 ms
26,076 KB
testcase_15 AC 173 ms
25,948 KB
testcase_16 AC 179 ms
25,940 KB
testcase_17 AC 169 ms
26,072 KB
testcase_18 AC 173 ms
26,072 KB
testcase_19 AC 187 ms
26,452 KB
testcase_20 AC 181 ms
26,072 KB
testcase_21 AC 190 ms
26,324 KB
権限があれば一括ダウンロードができます

ソースコード

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);
		ll ans = dist[x] + dist[y] - 2 * dist[xy];
		if (xy == x) swap(x, y);
		const int xz = hld.get_lca(x, z), yz = hld.get_lca(y, z);
		// y -> x
		if (xy == y) {
			if (xz == x) ans += dist[z] - dist[x];
			else if (yz == y) ans += dist[z] - dist[xz];
			else if (yz == z) ans += dist[y] - dist[z];
			else ans += dist[z] + dist[y] - 2 * dist[yz];
		}
		else {
			if (xz == z && yz == z) ans += dist[xy] - dist[z];
			else if (xz == x) ans += dist[z] - dist[x];
			else if (yz == y) ans += dist[z] - dist[y];
			else if (hld.get_lca(xz, yz) == xy) {
				if (xz != xy) ans += dist[z] - dist[xz];
				else ans += dist[z] - dist[yz];
			}
			else if (xz == z) ans += dist[xy] - dist[z];
			else ans += dist[z] + dist[xy] - 2 * dist[xz];
		}
		printf("%lld\n", ans);
	}
}
0