結果

問題 No.872 All Tree Path
ユーザー ngtkanangtkana
提出日時 2020-03-09 22:00:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 3,000 ms
コード長 746 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 172,176 KB
実行使用メモリ 25,316 KB
最終ジャッジ日時 2024-04-26 21:54:34
合計ジャッジ時間 3,663 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
18,084 KB
testcase_01 AC 136 ms
18,064 KB
testcase_02 AC 126 ms
18,088 KB
testcase_03 AC 87 ms
25,316 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 155 ms
18,144 KB
testcase_06 AC 127 ms
18,072 KB
testcase_07 AC 128 ms
18,120 KB
testcase_08 AC 11 ms
6,940 KB
testcase_09 AC 11 ms
6,940 KB
testcase_10 AC 11 ms
6,940 KB
testcase_11 AC 11 ms
6,944 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 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)->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