結果

問題 No.872 All Tree Path
ユーザー furafura
提出日時 2020-05-05 17:35:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 3,000 ms
コード長 806 bytes
コンパイル時間 2,394 ms
コンパイル使用メモリ 205,172 KB
実行使用メモリ 33,452 KB
最終ジャッジ日時 2023-09-09 05:07:58
合計ジャッジ時間 6,022 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
15,952 KB
testcase_01 AC 162 ms
15,860 KB
testcase_02 AC 160 ms
15,912 KB
testcase_03 AC 94 ms
33,452 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 158 ms
15,964 KB
testcase_06 AC 164 ms
15,952 KB
testcase_07 AC 174 ms
16,020 KB
testcase_08 AC 11 ms
4,388 KB
testcase_09 AC 11 ms
4,412 KB
testcase_10 AC 11 ms
4,420 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 11 ms
4,412 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
	G[v].emplace_back(u,wt);
}

int n;
weighted_graph<int> T;
vector<int> sz;

lint dfs(int u,int p){
	sz[u]=1;
	lint res=0;
	for(auto e:T[u]) if(e.to!=p) {
		int v=e.to;
		res+=dfs(v,u)+2LL*e.wt*sz[v]*(n-sz[v]);
		sz[u]+=sz[v];
	}
	return res;
}

int main(){
	scanf("%d",&n);
	T.resize(n);
	rep(i,n-1){
		int u,v,c; scanf("%d%d%d",&u,&v,&c); u--; v--;
		add_edge(T,u,v,c);
	}

	sz.resize(n);
	printf("%lld\n",dfs(0,-1));

	return 0;
}
0