結果

問題 No.872 All Tree Path
ユーザー MayimgMayimg
提出日時 2019-08-31 10:03:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 3,000 ms
コード長 632 bytes
コンパイル時間 4,076 ms
コンパイル使用メモリ 201,012 KB
実行使用メモリ 27,012 KB
最終ジャッジ日時 2023-08-15 19:48:12
合計ジャッジ時間 4,790 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
15,564 KB
testcase_01 AC 115 ms
15,412 KB
testcase_02 AC 116 ms
15,340 KB
testcase_03 AC 84 ms
27,012 KB
testcase_04 AC 5 ms
8,148 KB
testcase_05 AC 125 ms
15,324 KB
testcase_06 AC 126 ms
15,328 KB
testcase_07 AC 124 ms
15,372 KB
testcase_08 AC 13 ms
8,852 KB
testcase_09 AC 13 ms
8,764 KB
testcase_10 AC 13 ms
9,016 KB
testcase_11 AC 13 ms
8,760 KB
testcase_12 AC 13 ms
8,884 KB
testcase_13 AC 4 ms
8,072 KB
testcase_14 AC 4 ms
8,128 KB
testcase_15 AC 4 ms
8,032 KB
testcase_16 AC 4 ms
8,000 KB
testcase_17 AC 4 ms
8,108 KB
testcase_18 AC 4 ms
8,024 KB
testcase_19 AC 4 ms
8,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0