結果

問題 No.872 All Tree Path
ユーザー ShibuyapShibuyap
提出日時 2019-10-12 09:57:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 3,000 ms
コード長 2,510 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 175,496 KB
実行使用メモリ 30,824 KB
最終ジャッジ日時 2023-08-18 07:22:45
合計ジャッジ時間 5,202 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
30,632 KB
testcase_01 AC 302 ms
30,572 KB
testcase_02 AC 295 ms
30,632 KB
testcase_03 AC 189 ms
30,708 KB
testcase_04 AC 7 ms
15,072 KB
testcase_05 AC 295 ms
30,548 KB
testcase_06 AC 282 ms
30,824 KB
testcase_07 AC 297 ms
30,560 KB
testcase_08 AC 26 ms
16,620 KB
testcase_09 AC 26 ms
16,632 KB
testcase_10 AC 26 ms
16,892 KB
testcase_11 AC 28 ms
16,620 KB
testcase_12 AC 27 ms
16,824 KB
testcase_13 AC 7 ms
15,076 KB
testcase_14 AC 6 ms
15,108 KB
testcase_15 AC 7 ms
15,268 KB
testcase_16 AC 7 ms
15,108 KB
testcase_17 AC 7 ms
15,076 KB
testcase_18 AC 7 ms
15,320 KB
testcase_19 AC 7 ms
15,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

vector<P> G[MAX_N];
int par[MAX_N][2]; // 親、親と子の間のコスト
int cnt[MAX_N];
vector<P> sum[MAX_N];

int main() {
    int n;
    cin >> n;
    rep(i,n-1){
        int from, to, cost;
        cin >> from >> to >> cost;
        G[from].push_back(P(to,cost));
        G[to].push_back(P(from, cost));
    }

    // 根付き木に
    queue<int> que;
    que.push(1);
    par[1][0] = 200004;
    par[1][1] = 1;
    while(que.size() > 0){
        int x = que.front(); que.pop();
        rep(i,G[x].size()){
            if(par[G[x][i].first][0] == 0){
                par[G[x][i].first][0] = x;
                que.push(G[x][i].first);
                par[G[x][i].first][1] = G[x][i].second;
            }
        }
    }

    srep(i,2,n+1)cnt[par[i][0]]++;

    // ボトムアップに計算
    ll ans = 0;
    srep(i,1,n+1){
        if(cnt[i] == 0){
            que.push(i);
        }
    }
    while(que.size()>0){
        int x = que.front(); que.pop();
        if(x == 200004)break;
        if(cnt[x] != 0)continue;
        if(sum[x].size() == 0){ // 葉
            sum[par[x][0]].push_back(P(par[x][1], 1));
        }else{ // 計算
            ll tmp = 0;
            ll count = 0;
            rep(i, sum[x].size()){
                tmp += sum[x][i].first;
                count += sum[x][i].second;
            }
            count++;
            tmp += par[x][1] * count;
            sum[par[x][0]].push_back(P(tmp, count));
            count;
            tmp = 0;
            rep(i, sum[x].size()){
                tmp += sum[x][i].first * (count - sum[x][i].second);
            }
            ans += tmp;
        }
        cnt[par[x][0]]--;
        if(cnt[par[x][0]] == 0)que.push(par[x][0]);
        /*
        if(cnt[x] == 0){
            cout << x << " : ";
            rep(i,sum[x].size())cout << '(' << sum[x][i].first << ',' << sum[x][i].second << ')';
            cout << endl;
        }
        */
    }


    ans *= 2;
    cout << ans << endl;
    return 0;
}
 
 
0