結果
問題 | No.872 All Tree Path |
ユーザー |
![]() |
提出日時 | 2019-08-30 21:55:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 130 ms / 3,000 ms |
コード長 | 908 bytes |
コンパイル時間 | 1,866 ms |
コンパイル使用メモリ | 197,148 KB |
最終ジャッジ日時 | 2025-01-07 15:52:15 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>#define rep(i, a, n) for(int i = a; i < n; i++)#define int long longusing namespace std;typedef pair<int, int> P;const int mod = 1000000007;const int INF = 1e18;int n;vector<P> G[200010];int sz[200010];int ans = 0;int dfs(int v, int p){int cnt = 1;for(auto i : G[v]){if(i.first == p) continue;cnt += dfs(i.first, v);}sz[v] = cnt;return cnt;}void dfs2(int v, int p){for(auto i : G[v]){if(i.first == p) continue;ans += i.second * sz[i.first] * (n - sz[i.first]);dfs2(i.first, v);}}signed main(){cin.tie(nullptr);ios::sync_with_stdio(false);cin >> n;rep(i, 0, n - 1){int u, v, w;cin >> u >> v >> w;u--; v--;G[u].push_back({v, w});G[v].push_back({u, w});}dfs(0, -1);dfs2(0, -1);cout << ans * 2 << endl;}