結果

問題 No.1098 LCAs
ユーザー 👑 NachiaNachia
提出日時 2020-10-03 19:13:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 706 bytes
コンパイル時間 1,380 ms
コンパイル使用メモリ 166,716 KB
実行使用メモリ 27,160 KB
最終ジャッジ日時 2023-09-25 05:38:06
合計ジャッジ時間 5,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,080 KB
testcase_01 AC 3 ms
10,096 KB
testcase_02 AC 3 ms
10,084 KB
testcase_03 AC 3 ms
10,080 KB
testcase_04 AC 3 ms
10,044 KB
testcase_05 AC 3 ms
10,080 KB
testcase_06 AC 3 ms
10,232 KB
testcase_07 AC 3 ms
10,092 KB
testcase_08 AC 3 ms
10,028 KB
testcase_09 AC 3 ms
10,140 KB
testcase_10 AC 3 ms
10,236 KB
testcase_11 AC 3 ms
10,152 KB
testcase_12 AC 3 ms
10,080 KB
testcase_13 AC 3 ms
10,068 KB
testcase_14 AC 4 ms
10,084 KB
testcase_15 AC 4 ms
10,148 KB
testcase_16 AC 4 ms
10,156 KB
testcase_17 AC 4 ms
10,088 KB
testcase_18 AC 118 ms
18,604 KB
testcase_19 AC 123 ms
18,612 KB
testcase_20 AC 123 ms
18,644 KB
testcase_21 AC 117 ms
18,672 KB
testcase_22 AC 121 ms
18,584 KB
testcase_23 AC 88 ms
18,604 KB
testcase_24 AC 88 ms
18,724 KB
testcase_25 AC 83 ms
18,660 KB
testcase_26 AC 91 ms
18,612 KB
testcase_27 AC 86 ms
18,688 KB
testcase_28 AC 132 ms
26,332 KB
testcase_29 AC 132 ms
27,160 KB
testcase_30 AC 132 ms
26,568 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; scanf("%d", &N);
    rep(i, N - 1) {
        int u, v; scanf("%d%d", &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) printf("%lld\n", ans[i]);

    return 0;
}
0