結果

問題 No.872 All Tree Path
ユーザー otamay6otamay6
提出日時 2019-08-30 22:56:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 941 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 173,620 KB
実行使用メモリ 29,644 KB
最終ジャッジ日時 2023-08-14 07:11:44
合計ジャッジ時間 5,355 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 266 ms
19,300 KB
testcase_01 AC 263 ms
19,496 KB
testcase_02 AC 271 ms
19,904 KB
testcase_03 AC 188 ms
29,644 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 263 ms
19,412 KB
testcase_06 AC 264 ms
19,300 KB
testcase_07 AC 261 ms
19,552 KB
testcase_08 AC 20 ms
4,784 KB
testcase_09 AC 20 ms
4,784 KB
testcase_10 AC 20 ms
4,612 KB
testcase_11 AC 20 ms
4,660 KB
testcase_12 AC 20 ms
4,656 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,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(){
    long long N;cin>>N;
    typedef pair<int,long long> P;
    vector<vector<P>> graph(N);
    REP(i,N-1){
        int a,b; 
        long long 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<long long> leaf(N,0);
    auto dfs=[&](auto &&f,int i,int pre)->int{
        for(auto e:graph[i]){
            if(e.second==pre){
                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