結果

問題 No.872 All Tree Path
ユーザー heno239heno239
提出日時 2019-08-30 22:19:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 3,000 ms
コード長 2,666 bytes
コンパイル時間 1,146 ms
コンパイル使用メモリ 117,604 KB
実行使用メモリ 96,380 KB
最終ジャッジ日時 2023-08-14 05:45:13
合計ジャッジ時間 4,381 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 215 ms
22,596 KB
testcase_01 AC 219 ms
22,524 KB
testcase_02 AC 242 ms
22,556 KB
testcase_03 AC 170 ms
96,380 KB
testcase_04 AC 4 ms
11,656 KB
testcase_05 AC 230 ms
22,712 KB
testcase_06 AC 234 ms
22,572 KB
testcase_07 AC 236 ms
22,556 KB
testcase_08 AC 16 ms
12,396 KB
testcase_09 AC 16 ms
12,504 KB
testcase_10 AC 16 ms
12,452 KB
testcase_11 AC 17 ms
12,552 KB
testcase_12 AC 16 ms
12,492 KB
testcase_13 AC 4 ms
11,656 KB
testcase_14 AC 4 ms
11,520 KB
testcase_15 AC 4 ms
11,448 KB
testcase_16 AC 4 ms
11,596 KB
testcase_17 AC 5 ms
11,604 KB
testcase_18 AC 4 ms
11,552 KB
testcase_19 AC 5 ms
11,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;

struct edge {
	int to, cost;
};
vector<edge> G[1 << 18];


LP ansch[1 << 18];
void dfs(int id, int fr) {
	ll sum = 0;
	ll num = 1;
	rep(j, G[id].size()) {
		int to = G[id][j].to;
		if (to == fr)continue;
		dfs(to, id);
		num += ansch[to].second;
		sum += ansch[to].first + ansch[to].second*G[id][j].cost;
	}
	ansch[id] = { sum,num };
}
ll ans[1 << 18];
void dfs2(int id, int fr,LP cfr) {
	vector<int> ids;
	vector<int> costs;
	vector<LP> pre;
	rep(j, G[id].size()) {
		int to = G[id][j].to;
		if (to == fr)continue;
		ids.push_back(to); costs.push_back(G[id][j].cost);
		pre.push_back({ ansch[to].first + ansch[to].second*G[id][j].cost,ansch[to].second });
	}
	if (fr < 0) {
		ll sum = 0, num = 1;
		rep(i, ids.size()) {
			sum += pre[i].first;
			num += pre[i].second;
		}
		ans[id] = sum;
		rep(i, ids.size()) {
			int to = ids[i];
			ll tosum = sum - pre[i].first;
			ll tonum = num - pre[i].second;
			dfs2(to, id, { tosum + tonum * costs[i],tonum });
		}

	}
	else {
		pre.push_back(cfr);
		ll sum = 0, num = 1;
		rep(i, pre.size()) {
			sum += pre[i].first;
			num += pre[i].second;
		}
		ans[id] = sum;
		rep(i, ids.size()) {
			int to = ids[i];
			ll tosum = sum - pre[i].first;
			ll tonum = num - pre[i].second;
			dfs2(to, id, { tosum + tonum * costs[i],tonum });
		}
	}
}

void solve() {
	int n; cin >> n;
	rep(i, n-1) {
		int u, v, w; cin >> u >> v >> w; u--; v--;
		G[u].push_back({ v,w });
		G[v].push_back({ u,w });
	}
	dfs(0, -1); dfs2(0, -1, {});
	ll sum = 0;
	rep(i, n)sum += ans[i];
	/*rep(i, n) {
		cout << ans[i] << endl;
	}*/
	cout << sum << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(5);
	//init();
	solve();

	//cout << "finish" << endl;
	//stop
	return 0;
}
0