結果

問題 No.1098 LCAs
ユーザー finefine
提出日時 2020-06-26 22:03:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 818 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 171,076 KB
実行使用メモリ 30,152 KB
最終ジャッジ日時 2023-09-18 04:38:49
合計ジャッジ時間 5,117 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 140 ms
15,580 KB
testcase_19 AC 144 ms
15,908 KB
testcase_20 AC 138 ms
15,964 KB
testcase_21 AC 133 ms
15,840 KB
testcase_22 AC 128 ms
15,784 KB
testcase_23 AC 93 ms
16,156 KB
testcase_24 AC 91 ms
16,156 KB
testcase_25 AC 87 ms
16,444 KB
testcase_26 AC 98 ms
16,432 KB
testcase_27 AC 89 ms
16,500 KB
testcase_28 AC 166 ms
28,840 KB
testcase_29 AC 160 ms
30,152 KB
testcase_30 AC 165 ms
28,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

ll dfs(int cur, int par, const vector< vector<int> >& g, vector<ll>& ans) {
    ll sum = 1;
    for (int nex : g[cur]) {
        if (nex == par) continue;
        ll cnt = dfs(nex, cur, g, ans);
        sum += cnt;
        ans[cur] -= cnt * cnt;
    }
    ans[cur] += sum * sum;
    return sum;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector< vector<int> > g(n);
    for (int i = 1; i < n; i++) {
        int v, w;
        cin >> v >> w;
        --v; --w;
        g[v].push_back(w);
        g[w].push_back(v);
    }

    vector<ll> ans(n, 0);
    dfs(0, -1, g, ans);

    for (int i = 0; i < n; i++) {
        cout << ans[i] << newl;
    }

    return 0;
}
0