結果
問題 |
No.872 All Tree Path
|
ユーザー |
|
提出日時 | 2020-04-14 13:03:33 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 253 ms / 3,000 ms |
コード長 | 848 bytes |
コンパイル時間 | 2,110 ms |
コンパイル使用メモリ | 173,044 KB |
実行使用メモリ | 32,912 KB |
最終ジャッジ日時 | 2024-10-01 16:57:25 |
合計ジャッジ時間 | 5,161 ms |
ジャッジサーバー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; typedef pair<ll,ll> PL; typedef pair<int,int> P; const int INF = 1e9; const ll MOD = 1e9 + 7; const vector<int> dy = {-1,0,1,0}; const vector<int> dx = {0,1,0,-1}; const int MAXN = 200005; vector<vector<PL>> G(MAXN); ll ans = 0; ll dfs(ll i,ll n,ll p = -1) { ll ret = 1; for (PL pa:G[i]) { ll e = pa.first; ll c = pa.second; if (e == p) continue; ll tmp = dfs(e,n,i); ans += 2 * tmp * (n - tmp) * c; ret += tmp; } return ret; } int main() { ll n; cin >> n; rep(_,n - 1) { int a,b,c; cin >> a >> b >> c; a --; b--; G[a].push_back(PL(b,c)); G[b].push_back(PL(a,c)); } dfs(0,n); cout << ans << endl; return 0; }