結果

問題 No.1098 LCAs
ユーザー 👑 Nachia
提出日時 2020-10-03 19:12:11
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 460 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 1,620 ms
コンパイル使用メモリ 170,576 KB
実行使用メモリ 32,896 KB
最終ジャッジ日時 2024-07-18 04:49:00
合計ジャッジ時間 8,884 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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