結果

問題 No.872 All Tree Path
ユーザー sggkshiosggkshio
提出日時 2019-09-30 06:58:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 295 ms / 3,000 ms
コード長 953 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 95,424 KB
実行使用メモリ 34,276 KB
最終ジャッジ日時 2024-04-14 07:57:29
合計ジャッジ時間 5,189 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
19,552 KB
testcase_01 AC 295 ms
19,648 KB
testcase_02 AC 291 ms
19,700 KB
testcase_03 AC 203 ms
34,276 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 286 ms
19,492 KB
testcase_06 AC 292 ms
19,608 KB
testcase_07 AC 292 ms
19,504 KB
testcase_08 AC 22 ms
6,940 KB
testcase_09 AC 23 ms
6,944 KB
testcase_10 AC 21 ms
6,944 KB
testcase_11 AC 21 ms
6,944 KB
testcase_12 AC 21 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #



#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>


using namespace std;	
int n;
long long ans = 0;
vector<vector<pair<int, long long>>>p;
vector<long long>s;
void df(int cur, int prev, long long d) {
	for (int i = 0; i < p[cur].size(); i++) {
		int next = p[cur][i].first;
		if (next != prev)df(next, cur, p[cur][i].second);
	}
	if(prev == -1) return;
	s[prev] += s[cur];
	ans += d*s[cur] * (n - s[cur]) * 2;
}

int main() {


	cin >> n;
	
	p.resize(n);
	s.resize(n, 1);
	for (int i = 0; i < n-1; i++) {
		int a, b, d;
		cin >> a >> b >> d;
		a--; b--;
		p[a].push_back({b,d});
		p[b].push_back({a,d });
	}
	df(0, -1, -1);
	cout << ans << endl;

	return 0;




}
0