結果
問題 | No.872 All Tree Path |
ユーザー |
|
提出日時 | 2022-10-04 04:00:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 220 ms / 3,000 ms |
コード長 | 935 bytes |
コンパイル時間 | 2,338 ms |
コンパイル使用メモリ | 201,180 KB |
最終ジャッジ日時 | 2025-02-07 21:15:21 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for(int i = 0; i < (n); i++)using namespace std;typedef long long ll;int main(){cin.tie(0);ios::sync_with_stdio(0);int N; cin >> N;vector<vector<pair<int,int>>> G(N);rep(i,N-1) {int u,v,w; cin >> u >> v >> w; u--; v--;G[u].push_back({v, w});G[v].push_back({u, w});}vector<int> sz(N, 1);function<void(int,int)> get_sz = [&](int v, int p) -> void {for(auto [to, w] : G[v]) {if(to != p) {get_sz(to, v);sz[v] += sz[to];}}}; get_sz(0, -1);ll ans = 0;function<void(int,int)> dfs = [&](int v, int p) -> void {for(auto [to, w] : G[v]) {if(to != p) {ans += 1LL * w * sz[to] * (N - sz[to]);dfs(to, v);}}}; dfs(0, -1);cout << ans * 2 << endl;}