結果

問題 No.872 All Tree Path
ユーザー chocoruskchocorusk
提出日時 2019-08-30 21:40:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 231 ms / 3,000 ms
コード長 939 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 103,040 KB
実行使用メモリ 29,248 KB
最終ジャッジ日時 2024-05-01 17:00:54
合計ジャッジ時間 3,538 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 224 ms
17,860 KB
testcase_01 AC 216 ms
17,864 KB
testcase_02 AC 215 ms
17,988 KB
testcase_03 AC 180 ms
29,248 KB
testcase_04 AC 4 ms
9,028 KB
testcase_05 AC 231 ms
17,812 KB
testcase_06 AC 215 ms
17,860 KB
testcase_07 AC 212 ms
17,860 KB
testcase_08 AC 21 ms
9,668 KB
testcase_09 AC 21 ms
10,180 KB
testcase_10 AC 21 ms
9,796 KB
testcase_11 AC 21 ms
10,176 KB
testcase_12 AC 20 ms
10,308 KB
testcase_13 AC 3 ms
10,252 KB
testcase_14 AC 3 ms
8,644 KB
testcase_15 AC 3 ms
8,644 KB
testcase_16 AC 3 ms
8,644 KB
testcase_17 AC 3 ms
9,288 KB
testcase_18 AC 4 ms
9,288 KB
testcase_19 AC 4 ms
9,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int n;
vector<P> g[200020];
ll w[200020];
int cnt[200020];
bool used[200020];
ll ans;
void dfs(int x){
	used[x]=1;
	cnt[x]=1;
	for(auto p:g[x]){
		int y=p.first;
		if(used[y]) continue;
		dfs(y);
		cnt[x]+=cnt[y];
		ans+=2*(ll)(n-cnt[y])*(ll)cnt[y]*w[p.second];
	}
}
int main()
{
	cin>>n;
	for(int i=0; i<n-1; i++){
		int u, v; cin>>u>>v>>w[i];
		u--; v--;
		g[u].push_back({v, i});
		g[v].push_back({u, i});
	}
	dfs(0);
	cout<<ans<<endl;
	return 0;
}
0