結果

問題 No.872 All Tree Path
ユーザー otamay6otamay6
提出日時 2019-08-30 22:54:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 951 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 174,932 KB
実行使用メモリ 25,856 KB
最終ジャッジ日時 2024-05-01 19:49:20
合計ジャッジ時間 4,314 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
using namespace std;

int main(){
    int N;cin>>N;
    typedef pair<int,int> P;
    vector<vector<P>> graph(N);
    REP(i,N-1){
        int a,b,c; cin>>a>>b>>c;
        a--;b--;
        graph[a].push_back(P(c,b));
        graph[b].push_back(P(c,a));
    }
    long long ans=0;
    vector<int> leaf(N,0);
    vector<int> es(N,0);
    auto dfs=[&](auto &&f,int i,int pre)->int{
        for(auto e:graph[i]){
            if(e.second==pre){
                es[i]=e.first;
                continue;
            }
            leaf[i]+=f(f,e.second,i)+1;
        }
        for(auto e:graph[i]){
            if(e.second==pre) continue;
            ans+=(leaf[e.second]+1)*(N-leaf[e.second]-1)*e.first;
        }
        return leaf[i];
    };
    dfs(dfs,0,-1);
    
    cout<<2*ans<<endl;
}
0