結果

問題 No.872 All Tree Path
ユーザー Hoi_koroHoi_koro
提出日時 2019-08-30 22:00:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 3,000 ms
コード長 1,360 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 173,084 KB
実行使用メモリ 29,932 KB
最終ジャッジ日時 2023-08-14 05:01:24
合計ジャッジ時間 4,432 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 167 ms
19,504 KB
testcase_01 AC 165 ms
19,500 KB
testcase_02 AC 168 ms
19,628 KB
testcase_03 AC 85 ms
29,932 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 178 ms
19,640 KB
testcase_06 AC 160 ms
19,440 KB
testcase_07 AC 161 ms
19,560 KB
testcase_08 AC 11 ms
4,752 KB
testcase_09 AC 11 ms
4,684 KB
testcase_10 AC 11 ms
4,676 KB
testcase_11 AC 11 ms
4,676 KB
testcase_12 AC 11 ms
4,624 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  int n;
  vector<vector<pair<int, LL>>> t;
  vector<LL> cnt;
  Solver(LL n) : n(n), t(n), cnt(n){};

  LL dfs(int c, int p = -1) {
    DBG(c, p)
    LL ret = 0;
    LL subcnt = 1;
    LL len = 0;
    for (auto e : t[c]) {
      if (e.first != p) {
        ret += dfs(e.first, c);
        subcnt += cnt[e.first];
      } else {
        len = e.second;
      }
    }
    cnt[c] = subcnt;
    return ret + 2 * len * subcnt * (n - subcnt);
  }

  void solve() {
    for (int i = 0; i < n - 1; ++i) {
      int u, v, w;
      cin >> u >> v >> w;
      t[--u].emplace_back(--v, w);
      t[v].emplace_back(u, w);
    }
    cout << dfs(0) << endl;
  }
};

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}
0