結果

問題 No.872 All Tree Path
ユーザー tko919tko919
提出日時 2019-08-30 22:28:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 3,000 ms
コード長 1,290 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 174,236 KB
実行使用メモリ 37,028 KB
最終ジャッジ日時 2023-08-14 06:02:15
合計ジャッジ時間 4,431 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 179 ms
24,192 KB
testcase_01 AC 179 ms
24,612 KB
testcase_02 AC 176 ms
24,356 KB
testcase_03 AC 99 ms
37,028 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 180 ms
24,380 KB
testcase_06 AC 181 ms
24,344 KB
testcase_07 AC 176 ms
24,548 KB
testcase_08 AC 13 ms
5,208 KB
testcase_09 AC 12 ms
5,068 KB
testcase_10 AC 13 ms
5,208 KB
testcase_11 AC 13 ms
5,248 KB
testcase_12 AC 13 ms
5,064 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll; typedef pair<ll, ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename A,size_t N,typename T>void Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);}
const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2;
//template end

vector<vector<P>> g;
ll b[200010];
ll dfs(int v,int p){
    if(g[v].size()==0)return b[v]=1;
    ll res=1;
    for(P x:g[v])if(x.first!=p){
        res+=dfs(x.first,v);
    }
    return b[v]=res;
}

int main(){
    int n; scanf("%d",&n);
    g.resize(n); vector<pair<int,P>> es;
    rep(i,0,n-1){
        int u,v,w; scanf("%d%d%d",&u,&v,&w); u--; v--;
        g[u].push_back({v,w}); g[v].push_back({u,w});
        es.push_back({u,{v,w}});
    }
    ll ans=0; dfs(0,-1);
    rep(i,0,n-1){
        ll d=es[i].second.second;
        ll m=min(b[es[i].first],b[es[i].second.first]);
        ans+=m*(n-m)*d*2LL;
    }
    printf("%lld\n",ans);
    return 0;
}
0