結果

問題 No.872 All Tree Path
ユーザー ngtkana
提出日時 2020-03-09 22:00:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 185 ms / 3,000 ms
コード長 746 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 175,696 KB
実行使用メモリ 25,324 KB
最終ジャッジ日時 2024-11-14 13:29:06
合計ジャッジ時間 4,006 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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)->long long{
        long long sz=1;
        for(auto&&e:g.at(x)){
            int y;long long w;std::tie(y,w)=e;
            if(y==p)continue;
            long long s=dfs(dfs,y,x);
            ans+=2*s*(n-s)*w;
            sz+=s;
        }
        return sz;
    };
    dfs(dfs,0,0);
    std::cout<<ans<<'\n';
}
0