結果

問題 No.872 All Tree Path
ユーザー kappybarkappybar
提出日時 2020-04-08 17:19:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 3,000 ms
コード長 736 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 170,108 KB
実行使用メモリ 29,828 KB
最終ジャッジ日時 2023-09-25 22:01:18
合計ジャッジ時間 6,532 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
18,004 KB
testcase_01 AC 273 ms
17,716 KB
testcase_02 AC 276 ms
18,204 KB
testcase_03 AC 190 ms
29,828 KB
testcase_04 AC 5 ms
7,676 KB
testcase_05 AC 267 ms
17,992 KB
testcase_06 AC 268 ms
18,180 KB
testcase_07 AC 279 ms
17,712 KB
testcase_08 AC 24 ms
8,844 KB
testcase_09 AC 24 ms
8,760 KB
testcase_10 AC 23 ms
8,692 KB
testcase_11 AC 24 ms
8,712 KB
testcase_12 AC 22 ms
8,716 KB
testcase_13 AC 5 ms
7,464 KB
testcase_14 AC 4 ms
7,520 KB
testcase_15 AC 5 ms
7,476 KB
testcase_16 AC 5 ms
7,508 KB
testcase_17 AC 4 ms
7,440 KB
testcase_18 AC 5 ms
7,656 KB
testcase_19 AC 5 ms
7,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long ;
using P = pair<ll,ll> ;
const int INF = 1e9;
const int MOD = 1000000007;

int n = 200005;
vector<vector<P>> tree(n,vector<P>());
ll ans = 0;

ll dfs(int i,int before=-1){
    ll cnt = 0;
    for(P p:tree[i]){
        if(p.second == before) continue;
        ll c = dfs(p.second,i);
        ans += c * (n - c) * p.first;
        cnt += c;
    }
    return cnt + 1;
}


int main(){
    cin >> n;
    rep(i,n-1){
        int u,v;
        ll w;
        cin >> u >> v >> w;
        --u;--v;
        tree[u].push_back(P(w,v));
        tree[v].push_back(P(w,u));
    }

    dfs(0);
    ans *= 2;
    cout << ans << endl;
    return 0;
}
0