結果

問題 No.872 All Tree Path
ユーザー kimiyukikimiyuki
提出日時 2019-08-30 23:12:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 3,000 ms
コード長 1,233 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 209,664 KB
実行使用メモリ 30,988 KB
最終ジャッジ日時 2023-08-14 07:55:43
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 268 ms
18,136 KB
testcase_01 AC 277 ms
18,000 KB
testcase_02 AC 278 ms
18,084 KB
testcase_03 AC 190 ms
30,988 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 275 ms
18,048 KB
testcase_06 AC 265 ms
18,016 KB
testcase_07 AC 270 ms
18,096 KB
testcase_08 AC 20 ms
4,772 KB
testcase_09 AC 21 ms
4,788 KB
testcase_10 AC 21 ms
4,940 KB
testcase_11 AC 21 ms
4,572 KB
testcase_12 AC 20 ms
4,676 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 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;
template<typename Functor> struct fix_type { Functor functor; template<typename... Args> decltype(auto) operator() (Args && ... args) const & { return functor(functor, std::forward<Args>(args)...); } };
template<typename Functor> fix_type<typename std::decay<Functor>::type> fix(Functor && functor) { return { std::forward<Functor>(functor) }; }

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;
    fix([&](auto && self, 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 = self(self, j, i);
            sizes.push_back(size);
            d += 2ll * size * (n - size) * cost;
        }
        return accumulate(ALL(sizes), 0) + 1;
    })(0, -1);

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