結果

問題 No.872 All Tree Path
ユーザー leaf_1415
提出日時 2019-08-30 21:45:41
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 124 ms / 3,000 ms
コード長 759 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 64,856 KB
実行使用メモリ 29,876 KB
最終ジャッジ日時 2024-11-21 22:29:52
合計ジャッジ時間 2,643 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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