結果

問題 No.872 All Tree Path
ユーザー kuhaku
提出日時 2019-11-27 14:05:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 1,413 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 201,768 KB
最終ジャッジ日時 2025-01-08 05:40:12
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 2
other RE * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll dfs(ll, ll)’:
main.cpp:44:1: warning: no return statement in function returning non-void [-Wreturn-type]
   44 | }
      | ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
using P = pair<ll, ll>;
using Vec = vector<ll>;
#define FOR(i, m, n) for(ll (i) = (m); (i) < (n); (i)++)
#define FORN(i, m, n) for(ll (i) = (m); (i) <= (n); (i)++)
#define FORR(i, m, n) for(ll (i) = (m); (i) >= (n); (i)--)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n, 1)
#define co(n) cout << (n) << endl
#define cosp(n) cout << (n) << ' '
#define setp(n) cout << fixed << setprecision(n);
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second

const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;

vector<vector<P> > tree;
Vec child_num;
ll ans = 0;
ll n = 0;

ll dfs(ll node, ll par){
	ll cost = 0;
	for(P i : tree[node]){
		if(i.fs != par) dfs(i.fs, node);
		else cost = i.sc;
	}
	if(node != par){
		child_num[par] += child_num[node];
		ans += child_num[node]*(n-child_num[node])*cost*2;
	}
}

int main(void){
	cin >> n;
	tree.resize(n);
	child_num.resize(n);
	fill(all(child_num), 1);

	rep(i, n-1){
		ll u, v, w;
		cin >> u >> v >> w;
		u--, v--;
		tree[u].pb(mp(v, w));
		tree[v].pb(mp(u, w));
	}

	dfs(0, 0);
	co(ans);

	return 0;
}
0