結果

問題 No.1098 LCAs
ユーザー 👑 NachiaNachia
提出日時 2020-10-03 19:12:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 167,224 KB
実行使用メモリ 26,924 KB
最終ジャッジ日時 2023-09-25 05:37:50
合計ジャッジ時間 11,017 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,928 KB
testcase_01 AC 4 ms
9,916 KB
testcase_02 AC 9 ms
10,080 KB
testcase_03 AC 4 ms
9,916 KB
testcase_04 AC 4 ms
9,820 KB
testcase_05 AC 4 ms
9,824 KB
testcase_06 AC 4 ms
9,824 KB
testcase_07 AC 3 ms
10,004 KB
testcase_08 AC 4 ms
9,908 KB
testcase_09 AC 3 ms
9,844 KB
testcase_10 AC 3 ms
10,200 KB
testcase_11 AC 4 ms
10,080 KB
testcase_12 AC 4 ms
10,036 KB
testcase_13 AC 5 ms
10,088 KB
testcase_14 AC 6 ms
9,964 KB
testcase_15 AC 6 ms
9,900 KB
testcase_16 AC 5 ms
9,980 KB
testcase_17 AC 6 ms
10,016 KB
testcase_18 AC 478 ms
18,420 KB
testcase_19 AC 468 ms
18,692 KB
testcase_20 AC 467 ms
18,324 KB
testcase_21 AC 472 ms
18,404 KB
testcase_22 AC 457 ms
18,420 KB
testcase_23 AC 398 ms
18,728 KB
testcase_24 AC 400 ms
18,860 KB
testcase_25 AC 385 ms
18,604 KB
testcase_26 AC 422 ms
18,432 KB
testcase_27 AC 413 ms
18,536 KB
testcase_28 AC 503 ms
26,504 KB
testcase_29 AC 497 ms
26,924 KB
testcase_30 AC 495 ms
26,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const int xN = 200000;

int N;
vector<int> E[xN];
int P[xN];
LL Z[xN];
LL ans[xN];

void dfs(int p) {
    Z[p] = 1;
    for (int e : E[p]) {
        if (P[p] == e) continue;
        P[e] = p;
        dfs(e);
        Z[p] += Z[e];
        ans[p] -= Z[e] * Z[e];
    }
    ans[p] += Z[p] * Z[p];
}

int main() {
    int N; cin >> N;
    rep(i, N - 1) {
        int u, v; cin >> u >> v; u--; v--;
        E[u].push_back(v);
        E[v].push_back(u);
    }

    rep(i, N) P[i] = -1;
    dfs(0);

    rep(i, N) cout << ans[i] << endl;

    return 0;
}
0