結果
問題 | No.872 All Tree Path |
ユーザー | ptotq |
提出日時 | 2019-08-30 22:39:50 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 146 ms / 3,000 ms |
コード長 | 1,006 bytes |
コンパイル時間 | 1,468 ms |
コンパイル使用メモリ | 173,424 KB |
実行使用メモリ | 33,664 KB |
最終ジャッジ日時 | 2024-05-01 19:13:07 |
合計ジャッジ時間 | 3,500 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 138 ms
18,808 KB |
testcase_01 | AC | 135 ms
18,864 KB |
testcase_02 | AC | 140 ms
18,900 KB |
testcase_03 | AC | 90 ms
33,664 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 146 ms
18,944 KB |
testcase_06 | AC | 136 ms
18,840 KB |
testcase_07 | AC | 136 ms
18,944 KB |
testcase_08 | AC | 11 ms
5,376 KB |
testcase_09 | AC | 11 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 11 ms
5,376 KB |
testcase_12 | AC | 11 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 1 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> #define REP(i, start, end) for (int i=start, i##Len=(end); i < i##Len; ++i) using ll = int64_t; using namespace std; int N; vector<vector<pair<ll, ll>>> tree; int counts[200010] = {}; int rec(int v, int parent) { counts[v] = 1; for (auto p: tree[v]) { if (p.first == parent) continue; counts[v] += rec(p.first, v); } return counts[v]; } ll solve(int v, int parent) { ll total = 0; for (auto p : tree[v]) { if (p.first == parent) continue; total += p.second * counts[p.first] * (N-counts[p.first]) * 2 + solve(p.first, v); } return total; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cin >> N; tree = vector<vector<pair<ll, ll>>>(N+1); ll u, v, w; REP(i, 0, N-1) { cin >> u >> v >> w; tree[u].emplace_back(v, w); tree[v].emplace_back(u, w); } rec(1, 0); cout << solve(1, 0) << endl; return 0; }