結果

問題 No.872 All Tree Path
ユーザー face4face4
提出日時 2019-08-30 22:29:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 3,000 ms
コード長 926 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 74,384 KB
実行使用メモリ 25,676 KB
最終ジャッジ日時 2023-08-14 06:03:55
合計ジャッジ時間 4,209 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 270 ms
17,600 KB
testcase_01 AC 269 ms
17,356 KB
testcase_02 AC 265 ms
17,548 KB
testcase_03 AC 187 ms
25,676 KB
testcase_04 AC 7 ms
10,100 KB
testcase_05 AC 260 ms
17,580 KB
testcase_06 AC 261 ms
17,776 KB
testcase_07 AC 261 ms
17,564 KB
testcase_08 AC 24 ms
10,900 KB
testcase_09 AC 24 ms
11,044 KB
testcase_10 AC 24 ms
10,876 KB
testcase_11 AC 24 ms
10,856 KB
testcase_12 AC 24 ms
10,900 KB
testcase_13 AC 6 ms
10,104 KB
testcase_14 AC 6 ms
10,152 KB
testcase_15 AC 6 ms
9,988 KB
testcase_16 AC 6 ms
10,044 KB
testcase_17 AC 6 ms
9,888 KB
testcase_18 AC 6 ms
9,872 KB
testcase_19 AC 7 ms
10,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

vector<pair<int,int>> v[200000];
vector<int> child(200000, 0), par(200000, 0), dep(200000, 0);

int dfs(int pos, int bef){
    dep[pos] = bef==-1 ? 0 : dep[bef]+1;
    par[pos] = bef;
    child[pos] = 1;
    for(auto p : v[pos]){
        if(p.first != par[pos]){
            child[pos] += dfs(p.first, pos);
        }
    }
    return child[pos];
}

typedef long long ll;

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    dfs(0, -1);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        for(auto p : v[i]){
            ll num = (dep[i] > dep[p.first] ? child[i] : child[p.first]);
            ans += (ll)p.second * num * (n-num);
        }
    }
    cout << ans << endl;
    return 0;
}
0