結果

問題 No.872 All Tree Path
ユーザー ptotqptotq
提出日時 2019-08-30 22:32:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,012 bytes
コンパイル時間 1,731 ms
コンパイル使用メモリ 172,876 KB
実行使用メモリ 30,572 KB
最終ジャッジ日時 2024-05-01 18:55:20
合計ジャッジ時間 4,417 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, start, end) for (int i=start, i##Len=(end); i < i##Len; ++i)
using ll = int64_t;
using namespace std;

int N;
vector<vector<pair<int, int>>> tree;
int counts[200010] = {};
ll ans = 0;

int rec(int v, int parent) {
    for (auto p: tree[v]) {
        if (p.first == parent) continue;
        counts[v] += rec(p.first, v);
    }
    return counts[v]+1;
}

ll solve(int v, int parent) {
    ll total = 0;
    for (auto p : tree[v]) {
        if (p.first == parent) continue;
        total += p.second * (counts[p.first]+1) * (N-counts[p.first]-1) * 2 + solve(p.first, v);
    }
    return total;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    cin >> N;
    tree = vector<vector<pair<int, int>>>(N+1);

    int u, v, w;
    REP(i, 0, N-1) {
        cin >> u >> v >> w;
        tree[u].emplace_back(v, w);
        tree[v].emplace_back(u, w);
    }

    rec(1, 0);
    cout << solve(1, 0) << endl;

    return 0;
}
0