結果

問題 No.872 All Tree Path
ユーザー cotton_fn_cotton_fn_
提出日時 2019-08-30 22:30:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 3,000 ms
コード長 1,426 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 125,280 KB
実行使用メモリ 34,464 KB
最終ジャッジ日時 2023-08-14 06:05:56
合計ジャッジ時間 4,612 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 271 ms
19,836 KB
testcase_01 AC 269 ms
19,460 KB
testcase_02 AC 272 ms
19,508 KB
testcase_03 AC 186 ms
34,464 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 268 ms
19,460 KB
testcase_06 AC 265 ms
19,336 KB
testcase_07 AC 270 ms
19,244 KB
testcase_08 AC 21 ms
4,572 KB
testcase_09 AC 20 ms
4,688 KB
testcase_10 AC 20 ms
4,768 KB
testcase_11 AC 19 ms
4,688 KB
testcase_12 AC 20 ms
4,784 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <random>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

struct E {
  i64 v, w;
};

i64 n;
vector<vector<E>> g;
vector<i64> ch;
i64 ans;

void dfs(i64 u, i64 p) {
  ch[u] = 1;
  for (auto e : g[u]) {
    if (e.v != p) {
      dfs(e.v, u);
      ch[u] += ch[e.v];
      ans += (n - ch[e.v]) * ch[e.v] * e.w;
    }
  }
}

int main() {
  cin >> n;
  init_n(g, n + 1);

  for (i64 i = 0; i < n - 1; ++i) {
    i64 u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }

  init_n(ch, n + 1);
  dfs(1, -1);
  cout << 2 * ans << endl;
  
  return 0;
}
0