結果

問題 No.872 All Tree Path
ユーザー iqueue02iqueue02
提出日時 2019-10-23 21:43:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 3,000 ms
コード長 679 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 172,228 KB
実行使用メモリ 29,696 KB
最終ジャッジ日時 2023-09-21 10:39:15
合計ジャッジ時間 5,317 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 203 ms
15,152 KB
testcase_01 AC 200 ms
15,044 KB
testcase_02 AC 211 ms
15,168 KB
testcase_03 AC 187 ms
29,696 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 201 ms
14,984 KB
testcase_06 AC 203 ms
15,092 KB
testcase_07 AC 204 ms
15,048 KB
testcase_08 AC 19 ms
4,408 KB
testcase_09 AC 18 ms
4,392 KB
testcase_10 AC 18 ms
4,600 KB
testcase_11 AC 19 ms
4,380 KB
testcase_12 AC 19 ms
4,488 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;

Graph G;
ll res = 0;
ll dfs(int u, int p, ll n){
  ll c = 0;
  for (auto v : G[u]) {
    if (v.first == p) continue;
    ll d = dfs(v.first, u, n);
    res += v.second * d * (n - d);
    c += d;
  }
  return c + 1;
}

int main(){
  int n;
  cin >> n;
  G.resize(n);

  rep(i,n-1){
    int u, v, w;
    cin >> u >> v >> w;
    u--; v--;
    G[u].push_back({v,w});
    G[v].push_back({u,w});
  }
  dfs(0,-1,n);
  cout << res * 2<< endl;
  return 0;
}
0