結果

問題 No.872 All Tree Path
ユーザー risujirohrisujiroh
提出日時 2019-08-30 21:30:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 3,000 ms
コード長 755 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 172,596 KB
実行使用メモリ 23,288 KB
最終ジャッジ日時 2023-08-14 03:56:14
合計ジャッジ時間 3,964 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
16,620 KB
testcase_01 AC 122 ms
16,640 KB
testcase_02 AC 117 ms
16,568 KB
testcase_03 AC 77 ms
23,288 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 119 ms
16,696 KB
testcase_06 AC 128 ms
16,768 KB
testcase_07 AC 122 ms
16,688 KB
testcase_08 AC 11 ms
4,592 KB
testcase_09 AC 10 ms
4,380 KB
testcase_10 AC 11 ms
4,508 KB
testcase_11 AC 10 ms
4,384 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  struct Edge { int to, w; };
  VV<Edge> g(n);
  for (int _ = 0; _ < n - 1; ++_) {
    int u, v, w; cin >> u >> v >> w, --u, --v;
    g[u].emplace_back(Edge{v, w});
    g[v].emplace_back(Edge{u, w});
  }
  lint res = 0;
  V<lint> sz(n);
  auto dfs = [&](const auto& dfs, int v, int p) -> void {
    sz[v] = 1;
    for (const auto& e : g[v]) if (e.to != p) {
      dfs(dfs, e.to, v);
      res += sz[e.to] * (n - sz[e.to]) * e.w;
      sz[v] += sz[e.to];
    }
  };
  dfs(dfs, 0, -1);
  cout << 2 * res << '\n';
}
0