結果

問題 No.872 All Tree Path
ユーザー pekempeypekempey
提出日時 2019-08-30 21:36:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 3,000 ms
コード長 871 bytes
コンパイル時間 1,856 ms
コンパイル使用メモリ 175,884 KB
実行使用メモリ 31,992 KB
最終ジャッジ日時 2024-05-01 16:54:22
合計ジャッジ時間 3,753 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
18,808 KB
testcase_01 AC 139 ms
18,824 KB
testcase_02 AC 133 ms
18,836 KB
testcase_03 AC 90 ms
31,992 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 135 ms
18,892 KB
testcase_06 AC 138 ms
18,840 KB
testcase_07 AC 131 ms
18,896 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N; cin >> N;
  vector<vector<pair<ll, ll>>> G(N);
  rep(i, N - 1) {
    int a, b, c; cin >> a >> b >> c; a--; b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  vector<int> size(N);
  ll ans = 0;
  auto dfs = [&](auto dfs, int u, int p) -> void {
    size[u]++;
    for (auto e : G[u]) if (e.first != p) {
      dfs(dfs, e.first, u);
      size[u] += size[e.first];
    }
    for (auto e : G[u]) if (e.first != p) {
      ans += (ll)e.second * size[e.first] * (N - size[e.first]);
    }
  };
  dfs(dfs, 0, -1);
  cout << ans*2 << endl;
}
0