結果

問題 No.872 All Tree Path
ユーザー 👑 jupirojupiro
提出日時 2019-09-01 02:37:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 3,000 ms
コード長 1,466 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 105,892 KB
実行使用メモリ 37,496 KB
最終ジャッジ日時 2023-08-17 13:42:41
合計ジャッジ時間 4,883 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 221 ms
25,620 KB
testcase_01 AC 222 ms
25,728 KB
testcase_02 AC 220 ms
25,672 KB
testcase_03 AC 194 ms
37,496 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 223 ms
25,564 KB
testcase_06 AC 220 ms
25,612 KB
testcase_07 AC 219 ms
25,648 KB
testcase_08 AC 20 ms
5,432 KB
testcase_09 AC 20 ms
5,380 KB
testcase_10 AC 19 ms
5,680 KB
testcase_11 AC 19 ms
5,364 KB
testcase_12 AC 20 ms
5,524 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <fstream>
#include <bitset>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long INF = 1LL << 60;
const long long MOD = 1000000007LL;
const long long MAX = 500000LL;
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;


ll N;
vector<vector<pair<ll,ll>>> g1;
vector<ll> g2;
vector<ll> d;
ll dfs(ll x, ll pre, ll cur) {
	ll res = 0;
	d[x] = cur;
	for (pair<ll,ll> xx : g1[x]) {
		if (xx.first == pre) continue;
		res += dfs(xx.first, x, cur + 1) + 1;
	}

	return g2[x] = res;
}

int main()
{
	cin >> N;
	g1.resize(N);
	g2 = vector<ll>(N, 0);
	d.resize(N);
	vector<tuple<ll, ll, ll>> vt(N - 1);
	for (ll i = 0; i < N - 1; i++) {
		ll u, v, w;
		cin >> u >> v >> w;
		u--;
		v--;
		vt[i] = make_tuple(u, v, w);
		g1[u].push_back(make_pair(v, w));
		g1[v].push_back(make_pair(u, w));
	}

	dfs(0, -1, 0);

	ll res = 0;
	for (ll i = 0; i < N - 1; i++) {
		ll u, v, w;
		tie(u, v, w) = vt[i];
		if (d[u] > d[v]) {
			swap(u, v);
		}
		ll l = N - g2[v] - 1;
		ll r = g2[v] + 1;
		res += w * l * r * 2;
	}

	cout << res << endl;
	return 0;
}
0