結果

問題 No.872 All Tree Path
ユーザー KKT89KKT89
提出日時 2024-01-31 12:22:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 3,000 ms
コード長 950 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 221,524 KB
実行使用メモリ 30,404 KB
最終ジャッジ日時 2024-01-31 12:22:07
合計ジャッジ時間 5,775 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
19,780 KB
testcase_01 AC 115 ms
19,780 KB
testcase_02 AC 114 ms
19,780 KB
testcase_03 AC 86 ms
30,404 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 125 ms
19,780 KB
testcase_06 AC 142 ms
19,780 KB
testcase_07 AC 112 ms
19,780 KB
testcase_08 AC 10 ms
6,676 KB
testcase_09 AC 10 ms
6,676 KB
testcase_10 AC 11 ms
6,676 KB
testcase_11 AC 11 ms
6,676 KB
testcase_12 AC 12 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<pair<int, ll>>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        x -= 1, y -= 1;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }
    ll res = 0;
    vector<ll> sz(n);
    auto dfs = [&](auto dfs, int s, int p) -> void {
        sz[s] = 1;
        for (auto to : g[s]) {
            int t = to.first;
            if (t == p) continue;
            dfs(dfs, t, s);
            res += to.second * (sz[t]) * (n - sz[t]);
            sz[s] += sz[t];
        }
    };
    dfs(dfs, 0, -1);
    cout << res*2 << endl;
}
0