結果

問題 No.872 All Tree Path
ユーザー sprng_wlsprng_wl
提出日時 2019-08-30 23:10:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 287 ms / 3,000 ms
コード長 922 bytes
コンパイル時間 1,642 ms
コンパイル使用メモリ 169,976 KB
実行使用メモリ 34,320 KB
最終ジャッジ日時 2024-05-01 20:26:51
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 282 ms
19,712 KB
testcase_01 AC 287 ms
19,696 KB
testcase_02 AC 282 ms
19,676 KB
testcase_03 AC 204 ms
34,320 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 276 ms
19,672 KB
testcase_06 AC 272 ms
19,584 KB
testcase_07 AC 269 ms
19,560 KB
testcase_08 AC 21 ms
5,376 KB
testcase_09 AC 22 ms
5,376 KB
testcase_10 AC 21 ms
5,376 KB
testcase_11 AC 22 ms
5,376 KB
testcase_12 AC 21 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

void dfs(const vector<vector<pair<int, Int>>>& g, int u, vector<Int>& child) {
	child[u] = 1;
	for (auto p : g[u]) {
		int v = p.first;
		if (child[v] < 0) {
			dfs(g, v, child);
			child[u] += child[v];
		}
	}
}

int main() {
	Int N;
	cin >> N;
	vector<vector<pair<int, Int>>> g(N);
	for (int i = 0; i < N - 1; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		--u; --v;
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}

	vector<Int> child(N, -1);
	dfs(g, 0, child);

	Int ans = 0;
	deque<int> que;
	que.push_back(0);
	vector<bool> used(N, false);
	while (!que.empty()) {
		int u = que.front(); que.pop_front();
		if (used[u]) { continue; }
		used[u] = true;
		for (auto p : g[u]) {
			int v = p.first;
			if (used[v]) { continue; }
			ans += child[v] * (N - child[v]) * p.second * 2;
			que.push_back(v);
		}
	}
	cout << ans << endl;

	return 0;
}
0