結果

問題 No.898 tri-βutree
ユーザー 👑 jupirojupiro
提出日時 2020-01-22 11:20:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 4,000 ms
コード長 2,954 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 133,356 KB
実行使用メモリ 28,684 KB
最終ジャッジ日時 2024-04-26 09:11:30
合計ジャッジ時間 7,832 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
28,684 KB
testcase_01 AC 1 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 247 ms
25,088 KB
testcase_08 AC 243 ms
25,088 KB
testcase_09 AC 241 ms
25,088 KB
testcase_10 AC 242 ms
25,088 KB
testcase_11 AC 234 ms
25,088 KB
testcase_12 AC 243 ms
25,088 KB
testcase_13 AC 231 ms
25,088 KB
testcase_14 AC 240 ms
25,088 KB
testcase_15 AC 231 ms
25,088 KB
testcase_16 AC 244 ms
24,960 KB
testcase_17 AC 229 ms
25,052 KB
testcase_18 AC 235 ms
25,088 KB
testcase_19 AC 234 ms
25,088 KB
testcase_20 AC 226 ms
24,956 KB
testcase_21 AC 227 ms
24,960 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'll clz(ll)':
main.cpp:48:1: warning: control reaches end of non-void function [-Wreturn-type]
   48 | }
      | ^

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

ll N;
vector<vector<ll>> ug;
vector<vector<pair<ll, ll>>> wg;
vector<ll> sum;
ll clz(ll x) {
	for (ll i = 31; i >= 0; i--) {
		if ((x >> i) & 1) {
			return 31 - i;
		}
	}
}

template< typename G >
struct DoublingLowestCommonAncestor {
	const int LOG;
	vector< int > dep;
	const G& g;
	vector< vector< int > > table;

	DoublingLowestCommonAncestor(const G& g) : g(g), dep(g.size()), LOG(32 - clz(g.size())) {
		table.assign(LOG, vector< int >(g.size(), -1));
	}

	void dfs(int idx, int par, int d) {
		table[0][idx] = par;
		dep[idx] = d;
		for (auto& to : g[idx]) {
			if (to != par) dfs(to, idx, d + 1);
		}
	}

	void build() {
		dfs(0, -1, 0);
		for (int k = 0; k + 1 < LOG; k++) {
			for (int i = 0; i < table[k].size(); i++) {
				if (table[k][i] == -1) table[k + 1][i] = -1;
				else table[k + 1][i] = table[k][table[k][i]];
			}
		}
	}

	int query(int u, int v) {
		if (dep[u] > dep[v]) swap(u, v);
		for (int i = LOG - 1; i >= 0; i--) {
			if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
		}
		if (u == v) return u;
		for (int i = LOG - 1; i >= 0; i--) {
			if (table[i][u] != table[i][v]) {
				u = table[i][u];
				v = table[i][v];
			}
		}
		return table[0][u];
	}
};

void dfs(ll cur, ll pre) {
	for (auto next : wg[cur]) {
		if (next.first == pre) continue;
		sum[next.first] = sum[cur] + next.second;
		dfs(next.first, cur);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%lld", &N);
	ug.resize(N), wg.resize(N);
	for (ll i = 0; i < N - 1; i++) {
		ll x, y, w; scanf("%lld %lld %lld", &x, &y, &w);
		ug[x].push_back(y);
		ug[y].push_back(x);
		wg[x].emplace_back(y, w);
		wg[y].emplace_back(x, w);
	}
	sum = vector<ll>(N);
	dfs(0, -1);
	DoublingLowestCommonAncestor<vector<vector<ll>>> lca(ug);
	lca.build();
	ll Q; scanf("%lld", &Q);
	while (Q--) {
		ll x, y, z; scanf("%lld %lld %lld", &x, &y, &z);
		ll xy = lca.query(x, y);
		ll yz = lca.query(y, z);
		ll xz = lca.query(x, z);
		ll xyz = lca.query(xy, z);
		ll res = sum[x] + sum[y] + sum[z] - sum[xyz] * 3;
		res -= sum[xy] + sum[yz] + sum[xz] - sum[xyz] * 3;
		printf("%lld\n", res);
	}
	return 0;
}
0