結果

問題 No.872 All Tree Path
ユーザー leaf_1415leaf_1415
提出日時 2019-08-30 21:45:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 156 ms / 3,000 ms
コード長 759 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 66,648 KB
実行使用メモリ 30,084 KB
最終ジャッジ日時 2023-08-14 04:22:27
合計ジャッジ時間 3,470 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
18,420 KB
testcase_01 AC 148 ms
18,288 KB
testcase_02 AC 156 ms
18,236 KB
testcase_03 AC 89 ms
30,084 KB
testcase_04 AC 4 ms
8,032 KB
testcase_05 AC 152 ms
18,220 KB
testcase_06 AC 153 ms
18,224 KB
testcase_07 AC 155 ms
18,220 KB
testcase_08 AC 13 ms
9,064 KB
testcase_09 AC 14 ms
9,056 KB
testcase_10 AC 14 ms
9,144 KB
testcase_11 AC 14 ms
9,140 KB
testcase_12 AC 13 ms
9,124 KB
testcase_13 AC 4 ms
8,040 KB
testcase_14 AC 5 ms
8,228 KB
testcase_15 AC 4 ms
8,048 KB
testcase_16 AC 4 ms
8,068 KB
testcase_17 AC 5 ms
8,088 KB
testcase_18 AC 4 ms
8,032 KB
testcase_19 AC 5 ms
8,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>
#include <vector>
#define llint long long

using namespace std;

struct edge{
	llint to, cost;
	edge();
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint n;
vector<edge> G[200005];

llint ans = 0;
llint dfs(llint v, llint p)
{
	llint ret = 1;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].to == p) continue;
		llint res = dfs(G[v][i].to, v);
		ans += G[v][i].cost * res * (n-res);
		ret += res;
	}
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	llint u, v, w;
	for(int i = 0; i < n-1; i++){
		cin >> u >> v >> w;
		G[u].push_back(edge(v, w));
		G[v].push_back(edge(u, w));
	}
	
	dfs(1, -1);
	cout << ans*2 << endl;
	
	return 0;
}
0