結果
問題 | No.872 All Tree Path |
ユーザー |
|
提出日時 | 2019-08-30 21:51:15 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 255 ms / 3,000 ms |
コード長 | 592 bytes |
コンパイル時間 | 1,570 ms |
コンパイル使用メモリ | 172,040 KB |
実行使用メモリ | 23,712 KB |
最終ジャッジ日時 | 2024-11-21 22:41:36 |
合計ジャッジ時間 | 4,827 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>using namespace std;int N;vector<pair<int, int>> edges[200000];int64_t ans = 0;int dfs(int i, int p){int num = 1;for(auto& e : edges[i]){if(e.first == p) continue;int add = dfs(e.first, i);ans += 2LL * e.second * add * (N-add);num += add;}return num;}int main(){cin >> N;for(int i=0; i<N-1; i++){int a, b, c;cin >> a >> b >> c;edges[a-1].emplace_back(b-1, c);edges[b-1].emplace_back(a-1, c);}dfs(0, -1);cout << ans << endl;return 0;}