結果

問題 No.872 All Tree Path
ユーザー masayamatumasayamatu
提出日時 2019-09-23 13:04:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 275 ms / 3,000 ms
コード長 663 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 162,884 KB
実行使用メモリ 30,304 KB
最終ジャッジ日時 2023-10-19 08:06:45
合計ジャッジ時間 7,135 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
15,732 KB
testcase_01 AC 274 ms
15,728 KB
testcase_02 AC 274 ms
15,752 KB
testcase_03 AC 200 ms
30,304 KB
testcase_04 AC 4 ms
8,432 KB
testcase_05 AC 273 ms
15,732 KB
testcase_06 AC 275 ms
15,736 KB
testcase_07 AC 273 ms
15,732 KB
testcase_08 AC 23 ms
9,164 KB
testcase_09 AC 23 ms
9,168 KB
testcase_10 AC 23 ms
9,168 KB
testcase_11 AC 23 ms
9,172 KB
testcase_12 AC 23 ms
9,168 KB
testcase_13 AC 4 ms
8,432 KB
testcase_14 AC 4 ms
8,432 KB
testcase_15 AC 4 ms
8,436 KB
testcase_16 AC 4 ms
8,436 KB
testcase_17 AC 4 ms
8,436 KB
testcase_18 AC 4 ms
8,436 KB
testcase_19 AC 4 ms
8,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

struct edge{
    int to;
    int cost;
};

vector<edge> G[200000];

long dfs(int v,int p, int n, long &ans){
    long ret = 0;
    for(edge e:G[v]){
        int to = e.to;
        int cost = e.cost;
        if(to == p)continue;
        long a = dfs(to,v,n,ans);
        ans += cost*a*(n-a);
        ret += a;
    }
    return ret + 1;
}

int main(){
    int n = 0;
    cin >> n;
    for(int i = 0; i < n-1; i++){
        int a , b , c ;
        cin >> a >> b >> c;
        G[a].push_back({b , c});
        G[b].push_back({a , c});
    }
    long ans = 0;
    
    dfs(1,-1,n,ans);
    
    cout << ans * 2 << endl;
}
0