結果

問題 No.872 All Tree Path
ユーザー bonKotsubonKotsu
提出日時 2021-12-22 01:24:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 3,000 ms
コード長 947 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 201,936 KB
実行使用メモリ 27,612 KB
最終ジャッジ日時 2023-10-13 20:33:33
合計ジャッジ時間 6,519 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 279 ms
16,128 KB
testcase_01 AC 281 ms
16,160 KB
testcase_02 AC 284 ms
16,308 KB
testcase_03 AC 187 ms
27,612 KB
testcase_04 AC 5 ms
8,332 KB
testcase_05 AC 279 ms
16,100 KB
testcase_06 AC 277 ms
16,096 KB
testcase_07 AC 282 ms
16,112 KB
testcase_08 AC 23 ms
8,904 KB
testcase_09 AC 24 ms
8,900 KB
testcase_10 AC 22 ms
8,884 KB
testcase_11 AC 23 ms
8,840 KB
testcase_12 AC 23 ms
8,920 KB
testcase_13 AC 4 ms
7,992 KB
testcase_14 AC 5 ms
8,012 KB
testcase_15 AC 4 ms
8,020 KB
testcase_16 AC 4 ms
8,020 KB
testcase_17 AC 5 ms
8,096 KB
testcase_18 AC 5 ms
8,348 KB
testcase_19 AC 4 ms
8,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define LP pair<ll, ll>

template<class T>
void chmax(T& a, T b) { a = max(a, b); };
template<class T>
void chmin(T& a, T b) { a = min(a, b); };

struct Edge {
  int to, c;
  Edge(int to, int c) : to(to), c(c) {}
};

int n;
vector<Edge> G[200005];
int dp[200005];

void dfs(int v, int p=-1) {
  dp[v] = 1;
  for (auto e : G[v]) {
    if (e.to == p) continue;
    dfs(e.to, v);
    dp[v] += dp[e.to];
  }
}

ll ans = 0;
void dfs2(int v, int p=-1) {
  for (auto e : G[v]) {
    if (e.to == p) continue;
    ans += (ll)e.c*dp[e.to]*(n-dp[e.to]);
    dfs2(e.to, v);
  }


}

int main() {
  cin >> n;
  rep(i,n-1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    G[u].push_back(Edge(v,w));
    G[v].push_back(Edge(u,w));
  }
  dfs(0);

  dfs2(0);
  ans *= 2;
  cout << ans << endl;




  return 0;
}
0