結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-12-22 01:24:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 292 ms / 3,000 ms |
| コード長 | 947 bytes |
| コンパイル時間 | 2,012 ms |
| コンパイル使用メモリ | 196,956 KB |
| 最終ジャッジ日時 | 2025-01-27 04:55:23 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define LP pair<ll, ll>
template<class T>
void chmax(T& a, T b) { a = max(a, b); };
template<class T>
void chmin(T& a, T b) { a = min(a, b); };
struct Edge {
int to, c;
Edge(int to, int c) : to(to), c(c) {}
};
int n;
vector<Edge> G[200005];
int dp[200005];
void dfs(int v, int p=-1) {
dp[v] = 1;
for (auto e : G[v]) {
if (e.to == p) continue;
dfs(e.to, v);
dp[v] += dp[e.to];
}
}
ll ans = 0;
void dfs2(int v, int p=-1) {
for (auto e : G[v]) {
if (e.to == p) continue;
ans += (ll)e.c*dp[e.to]*(n-dp[e.to]);
dfs2(e.to, v);
}
}
int main() {
cin >> n;
rep(i,n-1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
dfs(0);
dfs2(0);
ans *= 2;
cout << ans << endl;
return 0;
}