結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
ptotq
|
| 提出日時 | 2019-08-30 22:32:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,012 bytes |
| コンパイル時間 | 1,937 ms |
| コンパイル使用メモリ | 172,488 KB |
| 実行使用メモリ | 30,592 KB |
| 最終ジャッジ日時 | 2024-11-22 00:51:21 |
| 合計ジャッジ時間 | 4,133 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 6 WA * 12 |
ソースコード
#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<int, int>>> tree;
int counts[200010] = {};
ll ans = 0;
int rec(int v, int parent) {
for (auto p: tree[v]) {
if (p.first == parent) continue;
counts[v] += rec(p.first, v);
}
return counts[v]+1;
}
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]+1) * (N-counts[p.first]-1) * 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<int, int>>>(N+1);
int 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;
}
ptotq