結果
問題 | No.1098 LCAs |
ユーザー |
![]() |
提出日時 | 2020-06-26 21:34:16 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 526 ms / 2,000 ms |
コード長 | 744 bytes |
コンパイル時間 | 1,600 ms |
コンパイル使用メモリ | 172,628 KB |
実行使用メモリ | 39,408 KB |
最終ジャッジ日時 | 2024-07-04 19:52:07 |
合計ジャッジ時間 | 9,463 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long int ll;typedef pair<ll, ll> P;typedef vector<ll> VI;typedef vector<VI> VVI;const ll MOD = 1000000007;const ll INF = 4611686018427387903;#define REP(i, n) for (int i = 0; i < n; i++)#define ALL(v) v.begin(), v.end()VVI edge(214514, VI(0));VI ans(214514,0);ll dfs(int v, int p){ll sum=0;VI a(0);for (auto to:edge[v]) {if (to!=p){ll k=dfs(to,v);sum+=k;a.push_back(k);}}ans[v]+=sum*2;for(auto x:a)ans[v]+=(sum-x)*x;ans[v]++;return sum+1;}int main(){int n; cin >> n;int a, b;REP(i,n-1){cin >> a >> b;a--; b--;edge[a].push_back(b);edge[b].push_back(a);}dfs(0,-1);REP(i,n)cout << ans[i] << endl;return 0;}