結果

問題 No.898 tri-βutree
ユーザー polylogKpolylogK
提出日時 2019-09-17 00:10:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 4,000 ms
コード長 2,651 bytes
コンパイル時間 2,430 ms
コンパイル使用メモリ 180,952 KB
実行使用メモリ 37,312 KB
最終ジャッジ日時 2024-04-26 08:19:13
合計ジャッジ時間 9,403 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
37,312 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 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 322 ms
34,880 KB
testcase_08 AC 322 ms
34,884 KB
testcase_09 AC 325 ms
34,752 KB
testcase_10 AC 334 ms
34,976 KB
testcase_11 AC 339 ms
35,004 KB
testcase_12 AC 339 ms
35,008 KB
testcase_13 AC 341 ms
34,884 KB
testcase_14 AC 319 ms
34,884 KB
testcase_15 AC 309 ms
34,880 KB
testcase_16 AC 309 ms
34,884 KB
testcase_17 AC 322 ms
34,880 KB
testcase_18 AC 313 ms
34,884 KB
testcase_19 AC 336 ms
34,876 KB
testcase_20 AC 323 ms
34,880 KB
testcase_21 AC 317 ms
34,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

class lowest_common_ancestor {
	std::vector<std::vector<size_t>> g, parent;
	std::vector<size_t> depth;
	
	const size_t logN = 20;

	private:
	void dfs(size_t v, int par = -1, size_t d = 0) {
		parent[0][v] = par;
		depth[v] = d;
		
		for(auto e:g[v]) {
			if(e == par) continue;
			
			dfs(e, v, d + 1);
		}
	}
	void build() {
		dfs(0);
		
		for(int k = 0; k < logN - 1; k++) {
			for(int i = 0; i < g.size(); i++) {
				if(parent[k][i] == -1) parent[k + 1][i] = -1;
				else parent[k + 1][i] = parent[k][parent[k][i]];
			}
		}
	}

	public:
	lowest_common_ancestor() {}
	lowest_common_ancestor(int n) : g(n), depth(n) {
		parent.assign(logN, std::vector<size_t>(n, -1));
	}
	void add_edge(const size_t & u, const size_t & v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}
	const size_t query(size_t u, size_t v) {
		static bool built = false;
		if(!built) {
			build();
			built = true;
		}

		if(depth[u] > depth[v]) std::swap(u, v);
		for(int k = 0; k < logN; k++) {
			if(((depth[v] - depth[u]) >> k) & 1) v = parent[k][v];
		}
		if(u == v) return u;

		for(int k = logN - 1; k >= 0; k--) {
			if(parent[k][u] == parent[k][v]) continue;

			u = parent[k][u];
			v = parent[k][v];			
		}		
		return parent[0][u];
	}
};


int main() {
	// input
	int n; scanf("%d", &n);
	assert(1 <= n and n <= (int)1e5);

	lowest_common_ancestor lca(n);
	std::vector<std::vector<std::pair<int, i64>>> g(n);
	for(int i = 0; i < n - 1; i++) {
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		assert(0 <= a and a < n);
		assert(0 <= b and b < n);
		assert(1 <= c and c <= (int)1e9);
		
		g[a].push_back({b, c});
		g[b].push_back({a, c});
		lca.add_edge(a, b);
	}
	
	// solve
	std::vector<i64> dist(n);
	auto dfs = [&](auto && dfs, int v, int par) -> void {
		for(auto e: g[v]) {
			if(e.first == par) continue;
			dist[e.first] = dist[v] + e.second;
			dfs(dfs, e.first, v);
		}
	};
	dfs(dfs, 0, -1);
	
	auto calc = [&lca, dist](const int & x, const int & y) -> i64 {
		return dist[x] + dist[y] - 2LL * dist[lca.query(x, y)];
	};

	int q; scanf("%d", &q);
	while(q--) {
		int x, y, z; scanf("%d%d%d", &x, &y, &z);
		assert(0 <= x and x < n);
		assert(0 <= y and y < n);
		assert(0 <= z and z < n);

		printf("%lld\n", (calc(x, y) + calc(y, z) + calc(z, x)) >> 1);
	}
	return 0;
}

// Θ((N + Q)logN)

0