結果

問題 No.872 All Tree Path
ユーザー Y17Y17
提出日時 2019-08-30 23:05:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 326 ms / 3,000 ms
コード長 1,058 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 147,676 KB
実行使用メモリ 47,296 KB
最終ジャッジ日時 2023-08-14 07:42:04
合計ジャッジ時間 6,250 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
31,272 KB
testcase_01 AC 326 ms
31,280 KB
testcase_02 AC 319 ms
31,256 KB
testcase_03 AC 212 ms
47,296 KB
testcase_04 AC 6 ms
12,868 KB
testcase_05 AC 317 ms
31,240 KB
testcase_06 AC 313 ms
31,556 KB
testcase_07 AC 322 ms
31,320 KB
testcase_08 AC 26 ms
14,728 KB
testcase_09 AC 26 ms
14,632 KB
testcase_10 AC 26 ms
14,724 KB
testcase_11 AC 26 ms
14,572 KB
testcase_12 AC 26 ms
14,632 KB
testcase_13 AC 5 ms
12,712 KB
testcase_14 AC 5 ms
13,008 KB
testcase_15 AC 5 ms
12,712 KB
testcase_16 AC 5 ms
12,808 KB
testcase_17 AC 6 ms
12,748 KB
testcase_18 AC 5 ms
13,008 KB
testcase_19 AC 5 ms
12,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long

vector<pair<int, int>> graph[200010];
vector<pair<int, int>> tree[200010];


int used[200010] = {};

void make_tree(int idx){
    used[idx] = 1;
    for(int i = 0;i < graph[idx].size();i++){
        int child = graph[idx][i].first;
        if(!used[child]){
            tree[idx].push_back({child, graph[idx][i].second});
            make_tree(child);

        }
    }
}

int memo[200010];
int n;

int dp(int idx){
    int ans = 0;
    int num = 1;

    for(int i = 0;i < tree[idx].size();i++){
        int child = tree[idx][i].first;
        ans += dp(child);
        num += memo[child];
        ans += (memo[child] * (n-memo[child]) * tree[idx][i].second) * 2;
    }

    memo[idx] = num;
    return ans;
}

signed main(){
    cin >> n;

    for(int i = 0;i < n-1;i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }

    make_tree(0);

    cout << dp(0) << endl;
    return 0;
}
0