結果

問題 No.872 All Tree Path
ユーザー SSRSSSRS
提出日時 2020-11-14 11:22:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 294 ms / 3,000 ms
コード長 1,024 bytes
コンパイル時間 1,891 ms
コンパイル使用メモリ 175,508 KB
実行使用メモリ 42,264 KB
最終ジャッジ日時 2023-09-30 04:48:32
合計ジャッジ時間 5,795 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
25,300 KB
testcase_01 AC 288 ms
25,368 KB
testcase_02 AC 294 ms
25,332 KB
testcase_03 AC 205 ms
42,264 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 286 ms
25,356 KB
testcase_06 AC 290 ms
25,264 KB
testcase_07 AC 292 ms
25,284 KB
testcase_08 AC 22 ms
5,096 KB
testcase_09 AC 22 ms
5,108 KB
testcase_10 AC 22 ms
5,568 KB
testcase_11 AC 23 ms
5,480 KB
testcase_12 AC 22 ms
5,540 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> &dp, vector<vector<pair<int, int>>> &c, int v = 0){
  dp[v] = 1;
  for (auto P : c[v]){
    int w = P.second;
    dfs(dp, c, w);
    dp[v] += dp[w];
  }
}
int main(){
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    E[u].push_back(make_pair(w, v));
    E[v].push_back(make_pair(w, u));
  }
  vector<int> p(N, -1);
  vector<vector<pair<int, int>>> c(N);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(P);
        Q.push(w);
      }
    }
  }
  vector<int> dp(N);
  dfs(dp, c);
  long long ans = 0;
  for (int i = 0; i < N; i++){
    for (auto P : c[i]){
      int d = P.first;
      int s = P.second;
      ans += (long long) d * dp[s] * (N - dp[s]);
    }
  }
  ans *= 2;
  cout << ans << endl;
}
0