結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-08-31 10:03:12 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 213 ms / 3,000 ms | 
| コード長 | 632 bytes | 
| コンパイル時間 | 2,229 ms | 
| コンパイル使用メモリ | 196,052 KB | 
| 最終ジャッジ日時 | 2025-01-07 16:08:34 | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int n;
vector<pair<int, int>> g[202020];
int dfs (int cur, int par) {
  int res = 0;
  for (auto nxt : g[cur]) {
    if (nxt.first == par) continue;
    int x = dfs(nxt.first, cur);
    res += x;
    ans += 1LL * x * (n - x) * nxt.second;
  }
  return res + 1;
}
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  dfs(0, -1);
  cout << ans * 2 << endl;
  return 0;
}
            
            
            
        