結果

問題 No.872 All Tree Path
ユーザー ngtkanangtkana
提出日時 2020-03-09 21:59:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 728 bytes
コンパイル時間 1,798 ms
コンパイル使用メモリ 171,808 KB
実行使用メモリ 24,708 KB
最終ジャッジ日時 2024-04-26 21:53:39
合計ジャッジ時間 4,869 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
6,944 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 11 ms
6,940 KB
testcase_09 AC 11 ms
6,940 KB
testcase_10 AC 12 ms
6,940 KB
testcase_11 AC 12 ms
6,940 KB
testcase_12 AC 12 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define endl enjoy_codeforces
int main(){
    std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);
    int n;std::cin>>n;
    std::vector<std::vector<std::pair<int,long long>>>g(n);
    for(int i=0;i<n-1;i++){
        int u,v;long long w;std::cin>>u>>v>>w;u--,v--;
        g.at(u).emplace_back(v,w);
        g.at(v).emplace_back(u,w);
    }
    long long ans=0;
    auto dfs=[&](auto&&dfs,int x,int p)->int{
        int sz=1;
        for(auto&&e:g.at(x)){
            int y;long long w;std::tie(y,w)=e;
            if(y==p)continue;
            int s=dfs(dfs,y,x);
            ans+=2*s*(n-s)*w;
            sz+=s;
        }
        return sz;
    };
    dfs(dfs,0,0);
    std::cout<<ans<<'\n';
}
0