結果
問題 |
No.1098 LCAs
|
ユーザー |
👑 ![]() |
提出日時 | 2020-10-03 19:13:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 196 ms / 2,000 ms |
コード長 | 706 bytes |
コンパイル時間 | 1,530 ms |
コンパイル使用メモリ | 169,672 KB |
実行使用メモリ | 32,976 KB |
最終ジャッジ日時 | 2024-07-18 04:49:14 |
合計ジャッジ時間 | 5,484 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#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; }