結果

問題 No.872 All Tree Path
ユーザー 👑 jupirojupiro
提出日時 2020-01-14 06:42:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 3,000 ms
コード長 1,992 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 122,440 KB
実行使用メモリ 31,208 KB
最終ジャッジ日時 2023-08-25 18:25:18
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
23,288 KB
testcase_01 AC 152 ms
23,288 KB
testcase_02 AC 139 ms
23,412 KB
testcase_03 AC 86 ms
31,208 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 146 ms
23,128 KB
testcase_06 AC 153 ms
22,992 KB
testcase_07 AC 153 ms
23,492 KB
testcase_08 AC 12 ms
5,040 KB
testcase_09 AC 12 ms
4,976 KB
testcase_10 AC 12 ms
4,980 KB
testcase_11 AC 12 ms
5,032 KB
testcase_12 AC 12 ms
5,152 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 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 <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

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 MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

ll N;
vector<vector<int>> g;
vector<ll> cnt;
vector<ll> h;

void dfs1(ll cur, ll pre) {
	for (auto next : g[cur]) {
		if (next == pre) continue;
		h[next] = h[cur] + 1;
		dfs1(next, cur);
	}
}

ll dfs2(ll cur, ll pre) {
	ll res = 1;
	for (auto next : g[cur]) {
		if (next == pre) continue;
		res += dfs2(next, cur);
	}
	return cnt[cur] = res;
}

class Edge {
public:
	ll source, target, cost;
	Edge(ll source = 0, ll target = 0, ll cost = 0) :
		source(source), target(target), cost(cost) {}
	bool operator<(const Edge &e)const {
		return cost < e.cost;
	}
};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%lld", &N);
	g.resize(N);
	cnt = vector<ll>(N);
	h = vector<ll>(N);
	vector<Edge> edges;
	for (ll i = 0; i < N - 1; i++) {
		ll u, v, w; scanf("%lld %lld %lld", &u, &v, &w);
		u--; v--;
		if (u > v) swap(u, v);
		g[u].emplace_back(v);
		g[v].emplace_back(u);
		edges.emplace_back(u, v, w);
	}
	dfs1(0, -1);
	dfs2(0, -1);
	ll res = 0;
	for (auto p : edges) {
		ll u = p.source;
		ll v = p.target;
		if (h[u] > h[v]) swap(u, v);
		res += cnt[v] * (N - cnt[v]) * p.cost * 2;
	}
	cout << res << endl;
	return 0;
}
0