結果

問題 No.386 貪欲な領主
ユーザー kazumakazuma
提出日時 2018-07-03 04:06:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 2,313 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 211,856 KB
実行使用メモリ 18,312 KB
最終ジャッジ日時 2023-09-13 17:11:46
合計ジャッジ時間 4,680 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 100 ms
18,248 KB
testcase_05 AC 126 ms
13,016 KB
testcase_06 AC 132 ms
13,036 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 15 ms
4,384 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 133 ms
13,076 KB
testcase_15 AC 86 ms
18,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class heavy_light_decomposition {
	const int n;
	vector<vector<int>> g;
	vector<int> in, out, size, head, par, dep, inv;
	int it;
	void erase_par(int v, int prev) {
		par[v] = prev;
		for (auto& u : g[v]) {
			if (u == g[v].back()) break;
			if (u == prev) swap(u, g[v].back());
			erase_par(u, v);
		}
		g[v].pop_back();
	}
	void dfs1(int v) {
		for (auto& u : g[v]) {
			dfs1(u);
			size[v] += size[u];
			if (size[u] > size[g[v][0]]) swap(u, g[v][0]);
		}
	}
	void dfs2(int v) {
		in[v] = it++;
		inv[in[v]] = v;
		for (auto u : g[v]) {
			head[u] = (u == g[v][0] ? head[v] : u);
			dep[u] = dep[v] + (u != g[v][0]);
			dfs2(u);
		}
		out[v] = it;
	}
public:
	heavy_light_decomposition(int n_)
		: n(n_), g(n), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), dep(n), inv(n), it(0) {}
	heavy_light_decomposition(const vector<vector<int>>& G)
		: n(G.size()), g(G), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), dep(n), inv(n), it(0) {}
	void add_edge(int u, int v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}
	void build(int rt = 0) {
		for (auto v : g[rt]) erase_par(v, rt);
		dfs1(rt);
		head[rt] = rt;
		dfs2(rt);
	}
	int get_id(int v) {
		return in[v];
	}
	void path_query(int u, int v, function<void(int, int)> f) {
		while (true) {
			if (in[u] > in[v]) swap(u, v);
			f(max(in[head[v]], in[u]), in[v] + 1);
			if (head[u] == head[v]) return;
			v = par[head[v]];
		}
	}
	void subtree_query(int v, function<void(int, int)> f) {
		f(in[v], out[v]);
	}
	int get_lca(int u, int v) {
		while (true) {
			if (in[u] > in[v]) swap(u, v);
			if (head[u] == head[v]) return u;
			v = par[head[v]];
		}
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	heavy_light_decomposition hl(N);
	for (int i = 0; i < N - 1; i++) {
		int A, B;
		cin >> A >> B;
		hl.add_edge(A, B);
	}
	hl.build();
	vector<ll> u(N), sum(N + 1);
	for (int i = 0; i < N; i++) {
		int U;
		cin >> U;
		u[hl.get_id(i)] = U;
	}
	for (int i = 0; i < N; i++) {
		sum[i + 1] = sum[i] + u[i];
	}
	int M;
	cin >> M;
	ll res = 0;
	while (M--) {
		int A, B;
		ll C;
		cin >> A >> B >> C;
		ll all = 0;
		hl.path_query(A, B, [&](int l, int r) {
			all += sum[r] - sum[l];
		});
		res += all * C;
	}
	cout << res << endl;
	return 0;
}
0