結果

問題 No.872 All Tree Path
ユーザー finefine
提出日時 2019-08-30 21:53:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 3,000 ms
コード長 782 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 171,924 KB
実行使用メモリ 32,892 KB
最終ジャッジ日時 2024-05-01 17:34:03
合計ジャッジ時間 3,244 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
17,920 KB
testcase_01 AC 115 ms
18,048 KB
testcase_02 AC 117 ms
18,080 KB
testcase_03 AC 83 ms
32,892 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 120 ms
18,136 KB
testcase_06 AC 119 ms
18,048 KB
testcase_07 AC 114 ms
18,048 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 9 ms
5,376 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 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>

using namespace std;

using ll = long long;
using P = pair<int, ll>;

ll solve(int cur, int par, vector< vector<P> >& g, ll& ans) {
    ll n = g.size();

    ll res = 1;
    for (P& e : g[cur]) {
        if (e.first == par) continue;
        ll num = solve(e.first, cur, g, ans);
        res += num;
        ans += e.second * num * (n - num) * 2;
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector< vector<P> > g(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    ll ans = 0;
    solve(0, -1, g, ans);
    cout << ans << endl;
    return 0;
}
0