結果

問題 No.872 All Tree Path
ユーザー kk
提出日時 2020-06-28 06:15:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 3,000 ms
コード長 687 bytes
コンパイル時間 2,053 ms
コンパイル使用メモリ 202,344 KB
実行使用メモリ 26,840 KB
最終ジャッジ日時 2023-09-21 03:12:59
合計ジャッジ時間 5,781 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
15,392 KB
testcase_01 AC 162 ms
15,376 KB
testcase_02 AC 162 ms
15,404 KB
testcase_03 AC 82 ms
26,840 KB
testcase_04 AC 4 ms
8,060 KB
testcase_05 AC 162 ms
15,448 KB
testcase_06 AC 163 ms
15,388 KB
testcase_07 AC 167 ms
15,332 KB
testcase_08 AC 13 ms
8,824 KB
testcase_09 AC 13 ms
8,844 KB
testcase_10 AC 14 ms
8,820 KB
testcase_11 AC 13 ms
8,856 KB
testcase_12 AC 13 ms
8,812 KB
testcase_13 AC 5 ms
8,180 KB
testcase_14 AC 4 ms
8,088 KB
testcase_15 AC 5 ms
8,060 KB
testcase_16 AC 4 ms
8,084 KB
testcase_17 AC 4 ms
8,232 KB
testcase_18 AC 4 ms
8,224 KB
testcase_19 AC 4 ms
8,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

vector<pair<int, int > > g[200000];
int n;
long long ret;

int dfs(int v, int par = -1) {
  int sub = 1; // me
  for (auto &pr: g[v]) {
    int u = pr.first;
    int w = pr.second;
    if (u == par)
      continue;
    int tmp = dfs(u, v);
    ret += (long long)tmp * (n - tmp) * w;
    sub += tmp;
  }
  return sub;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  REP (i, n-1) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  ret = 0;
  dfs(0);
  cout << 2 * ret << endl;
  
  return 0;
}
0