結果

問題 No.872 All Tree Path
ユーザー leaf_1415leaf_1415
提出日時 2019-08-30 21:45:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 181 ms / 3,000 ms
コード長 759 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 65,092 KB
実行使用メモリ 29,952 KB
最終ジャッジ日時 2024-05-01 17:10:19
合計ジャッジ時間 3,487 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 181 ms
18,248 KB
testcase_01 AC 180 ms
18,176 KB
testcase_02 AC 178 ms
18,212 KB
testcase_03 AC 87 ms
29,952 KB
testcase_04 AC 4 ms
8,000 KB
testcase_05 AC 177 ms
18,176 KB
testcase_06 AC 170 ms
18,176 KB
testcase_07 AC 174 ms
18,084 KB
testcase_08 AC 14 ms
9,060 KB
testcase_09 AC 14 ms
9,008 KB
testcase_10 AC 14 ms
9,088 KB
testcase_11 AC 15 ms
9,052 KB
testcase_12 AC 14 ms
8,896 KB
testcase_13 AC 4 ms
8,064 KB
testcase_14 AC 4 ms
7,996 KB
testcase_15 AC 5 ms
8,044 KB
testcase_16 AC 4 ms
8,064 KB
testcase_17 AC 5 ms
7,996 KB
testcase_18 AC 4 ms
7,908 KB
testcase_19 AC 5 ms
8,064 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