結果
問題 | No.1098 LCAs |
ユーザー |
![]() |
提出日時 | 2020-06-26 21:28:57 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 186 ms / 2,000 ms |
コード長 | 882 bytes |
コンパイル時間 | 2,000 ms |
コンパイル使用メモリ | 200,348 KB |
最終ジャッジ日時 | 2025-01-11 10:49:07 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> using Pa = pair<T, U>; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vvec<int> g(N); for(int i=0;i<N-1;i++){ int a,b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vec<ll> ans(N),s(N); auto dfs = [&](auto&& self,int cur,int par)->void{ ll sum = 0,sum2 = 0; for(auto& to:g[cur]) if(to!=par){ self(self,to,cur); sum += s[to]; sum2 += s[to]*s[to]; s[cur] += s[to]; } s[cur]++; ans[cur] = (sum*sum-sum2)+2*s[cur]-1; }; dfs(dfs,0,-1); for(int i=0;i<N;i++) cout << ans[i] << "\n"; }