結果

問題 No.872 All Tree Path
ユーザー betrue12betrue12
提出日時 2019-08-30 21:51:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 3,000 ms
コード長 592 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 168,788 KB
実行使用メモリ 23,772 KB
最終ジャッジ日時 2023-08-14 04:32:50
合計ジャッジ時間 5,036 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
15,340 KB
testcase_01 AC 250 ms
15,596 KB
testcase_02 AC 245 ms
15,352 KB
testcase_03 AC 183 ms
23,772 KB
testcase_04 AC 4 ms
8,036 KB
testcase_05 AC 242 ms
15,336 KB
testcase_06 AC 249 ms
15,460 KB
testcase_07 AC 245 ms
15,296 KB
testcase_08 AC 22 ms
8,776 KB
testcase_09 AC 22 ms
8,780 KB
testcase_10 AC 22 ms
8,780 KB
testcase_11 AC 22 ms
8,768 KB
testcase_12 AC 22 ms
9,052 KB
testcase_13 AC 5 ms
8,080 KB
testcase_14 AC 4 ms
8,048 KB
testcase_15 AC 5 ms
8,176 KB
testcase_16 AC 4 ms
8,220 KB
testcase_17 AC 5 ms
8,044 KB
testcase_18 AC 4 ms
8,088 KB
testcase_19 AC 4 ms
8,036 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