結果
問題 | No.872 All Tree Path |
ユーザー |
|
提出日時 | 2019-08-30 22:29:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 283 ms / 3,000 ms |
コード長 | 926 bytes |
コンパイル時間 | 702 ms |
コンパイル使用メモリ | 75,124 KB |
実行使用メモリ | 25,844 KB |
最終ジャッジ日時 | 2024-11-22 00:45:36 |
合計ジャッジ時間 | 3,968 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include<iostream> #include<vector> #include<queue> using namespace std; vector<pair<int,int>> v[200000]; vector<int> child(200000, 0), par(200000, 0), dep(200000, 0); int dfs(int pos, int bef){ dep[pos] = bef==-1 ? 0 : dep[bef]+1; par[pos] = bef; child[pos] = 1; for(auto p : v[pos]){ if(p.first != par[pos]){ child[pos] += dfs(p.first, pos); } } return child[pos]; } typedef long long ll; int main(){ int n; cin >> n; for(int i = 0; i < n-1; i++){ int a, b, c; cin >> a >> b >> c; a--, b--; v[a].push_back({b, c}); v[b].push_back({a, c}); } dfs(0, -1); ll ans = 0; for(int i = 0; i < n; i++){ for(auto p : v[i]){ ll num = (dep[i] > dep[p.first] ? child[i] : child[p.first]); ans += (ll)p.second * num * (n-num); } } cout << ans << endl; return 0; }