結果

問題 No.1098 LCAs
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-07-05 11:04:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 521 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 175,116 KB
実行使用メモリ 31,212 KB
最終ジャッジ日時 2023-10-22 05:04:46
合計ジャッジ時間 11,444 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 480 ms
16,692 KB
testcase_19 AC 478 ms
16,692 KB
testcase_20 AC 484 ms
16,692 KB
testcase_21 AC 480 ms
16,692 KB
testcase_22 AC 477 ms
16,692 KB
testcase_23 AC 405 ms
17,232 KB
testcase_24 AC 402 ms
17,236 KB
testcase_25 AC 399 ms
17,456 KB
testcase_26 AC 433 ms
17,456 KB
testcase_27 AC 420 ms
17,456 KB
testcase_28 AC 521 ms
29,892 KB
testcase_29 AC 510 ms
31,212 KB
testcase_30 AC 511 ms
29,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int n;
vector<vector<int>> hen;
vector<int> dp;
vector<ll> ans;

int dfs(int v, int par) {
    
    if(dp[v])return dp[v];

    ll sum = 1;
    ll res = 0;

    for(auto i : hen[v]) {
        if(i == par)continue;
        ll cur = dfs(i, v);
        sum += cur;
        res -= cur * cur;
    }

    res += (sum) * sum;
    ans[v] =res;

    return dp[v] = sum;

}


int main()
{
    cin >> n;
    hen.resize(n), dp.resize(n), ans.resize(n);
    for(int i = 0; i < n - 1; i++) {
        int s, t; cin >> s >> t; s--, t--;
        hen[s].emplace_back(t);
        hen[t].emplace_back(s);
    }
    dfs(0, -1);

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

}
0