結果

問題 No.872 All Tree Path
ユーザー betrue12betrue12
提出日時 2019-08-30 21:51:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 3,000 ms
コード長 592 bytes
コンパイル時間 1,617 ms
コンパイル使用メモリ 171,944 KB
実行使用メモリ 23,724 KB
最終ジャッジ日時 2024-05-01 17:20:30
合計ジャッジ時間 4,115 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 214 ms
15,300 KB
testcase_01 AC 211 ms
15,552 KB
testcase_02 AC 198 ms
15,424 KB
testcase_03 AC 183 ms
23,724 KB
testcase_04 AC 4 ms
8,108 KB
testcase_05 AC 196 ms
15,484 KB
testcase_06 AC 203 ms
15,432 KB
testcase_07 AC 199 ms
15,360 KB
testcase_08 AC 22 ms
8,704 KB
testcase_09 AC 21 ms
8,704 KB
testcase_10 AC 22 ms
8,832 KB
testcase_11 AC 21 ms
8,832 KB
testcase_12 AC 20 ms
8,788 KB
testcase_13 AC 4 ms
8,064 KB
testcase_14 AC 4 ms
8,320 KB
testcase_15 AC 5 ms
8,064 KB
testcase_16 AC 4 ms
8,040 KB
testcase_17 AC 4 ms
8,192 KB
testcase_18 AC 4 ms
8,192 KB
testcase_19 AC 4 ms
8,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int N;
vector<pair<int, int>> edges[200000];

int64_t ans = 0;
int dfs(int i, int p){
    int num = 1;
    for(auto& e : edges[i]){
        if(e.first == p) continue;
        int add = dfs(e.first, i);
        ans += 2LL * e.second * add * (N-add);
        num += add;
    }
    return num;
}

int main(){
    cin >> N;
    for(int i=0; i<N-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        edges[a-1].emplace_back(b-1, c);
        edges[b-1].emplace_back(a-1, c);
    }

    dfs(0, -1);
    cout << ans << endl;
    return 0;
}
0