結果
問題 | No.872 All Tree Path |
ユーザー | ptotq |
提出日時 | 2019-08-30 22:39:50 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 198 ms / 3,000 ms |
コード長 | 1,006 bytes |
コンパイル時間 | 1,516 ms |
コンパイル使用メモリ | 171,744 KB |
実行使用メモリ | 33,572 KB |
最終ジャッジ日時 | 2023-08-14 06:27:52 |
合計ジャッジ時間 | 4,248 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 184 ms
18,732 KB |
testcase_01 | AC | 183 ms
18,744 KB |
testcase_02 | AC | 188 ms
18,684 KB |
testcase_03 | AC | 91 ms
33,572 KB |
testcase_04 | AC | 2 ms
4,380 KB |
testcase_05 | AC | 198 ms
19,008 KB |
testcase_06 | AC | 193 ms
18,792 KB |
testcase_07 | AC | 190 ms
18,804 KB |
testcase_08 | AC | 11 ms
4,692 KB |
testcase_09 | AC | 12 ms
4,556 KB |
testcase_10 | AC | 11 ms
4,556 KB |
testcase_11 | AC | 12 ms
4,672 KB |
testcase_12 | AC | 12 ms
4,556 KB |
testcase_13 | AC | 2 ms
4,376 KB |
testcase_14 | AC | 1 ms
4,376 KB |
testcase_15 | AC | 1 ms
4,380 KB |
testcase_16 | AC | 1 ms
4,380 KB |
testcase_17 | AC | 1 ms
4,376 KB |
testcase_18 | AC | 2 ms
4,376 KB |
testcase_19 | AC | 2 ms
4,380 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; }