結果
問題 | No.872 All Tree Path |
ユーザー |
|
提出日時 | 2019-09-23 13:04:47 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 234 ms / 3,000 ms |
コード長 | 663 bytes |
コンパイル時間 | 1,986 ms |
コンパイル使用メモリ | 162,380 KB |
実行使用メモリ | 29,860 KB |
最終ジャッジ日時 | 2024-09-19 04:22:45 |
合計ジャッジ時間 | 5,370 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include<bits/stdc++.h>using namespace std;struct edge{int to;int cost;};vector<edge> G[200000];long dfs(int v,int p, int n, long &ans){long ret = 0;for(edge e:G[v]){int to = e.to;int cost = e.cost;if(to == p)continue;long a = dfs(to,v,n,ans);ans += cost*a*(n-a);ret += a;}return ret + 1;}int main(){int n = 0;cin >> n;for(int i = 0; i < n-1; i++){int a , b , c ;cin >> a >> b >> c;G[a].push_back({b , c});G[b].push_back({a , c});}long ans = 0;dfs(1,-1,n,ans);cout << ans * 2 << endl;}