結果

問題 No.872 All Tree Path
ユーザー kimiyukikimiyuki
提出日時 2019-08-30 23:13:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 3,000 ms
コード長 898 bytes
コンパイル時間 2,036 ms
コンパイル使用メモリ 210,548 KB
実行使用メモリ 42,324 KB
最終ジャッジ日時 2024-05-01 20:34:04
合計ジャッジ時間 4,792 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 232 ms
18,088 KB
testcase_01 AC 233 ms
18,136 KB
testcase_02 AC 226 ms
18,172 KB
testcase_03 AC 195 ms
42,324 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 227 ms
18,104 KB
testcase_06 AC 232 ms
18,220 KB
testcase_07 AC 252 ms
18,084 KB
testcase_08 AC 21 ms
6,944 KB
testcase_09 AC 21 ms
6,940 KB
testcase_10 AC 21 ms
6,940 KB
testcase_11 AC 21 ms
6,940 KB
testcase_12 AC 21 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;

int main() {
    // input
    int n; cin >> n;
    vector<vector<pair<int, ll> > > g(n);
    REP (i, n - 1) {
        int u, v, w; cin >> u >> v >> w;
        -- u;
        -- v;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    // solve
    ll d = 0;
    function<int (int, int)> go = [&](int i, int parent) -> int {
        vector<int> sizes;
        for (auto edge : g[i]) {
            int j, cost; tie(j, cost) = edge;
            if (j == parent) continue;
            int size = go(j, i);
            sizes.push_back(size);
            d += 2ll * size * (n - size) * cost;
        }
        return accumulate(ALL(sizes), 0) + 1;
    };
    go(0, -1);

    // output
    cout << d << endl;
    return 0;
}
0