結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  iqueue02 | 
| 提出日時 | 2019-10-23 21:43:45 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 201 ms / 3,000 ms | 
| コード長 | 679 bytes | 
| コンパイル時間 | 1,662 ms | 
| コンパイル使用メモリ | 173,224 KB | 
| 実行使用メモリ | 29,756 KB | 
| 最終ジャッジ日時 | 2024-07-07 04:42:43 | 
| 合計ジャッジ時間 | 5,136 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;
Graph G;
ll res = 0;
ll dfs(int u, int p, ll n){
  ll c = 0;
  for (auto v : G[u]) {
    if (v.first == p) continue;
    ll d = dfs(v.first, u, n);
    res += v.second * d * (n - d);
    c += d;
  }
  return c + 1;
}
int main(){
  int n;
  cin >> n;
  G.resize(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,n);
  cout << res * 2<< endl;
  return 0;
}
            
            
            
        