結果
問題 |
No.1098 LCAs
|
ユーザー |
![]() |
提出日時 | 2020-09-21 21:52:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 503 ms / 2,000 ms |
コード長 | 771 bytes |
コンパイル時間 | 1,625 ms |
コンパイル使用メモリ | 168,784 KB |
実行使用メモリ | 34,944 KB |
最終ジャッジ日時 | 2024-06-24 20:32:35 |
合計ジャッジ時間 | 10,407 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define N (1000000000+7) //#define N 998244353 #define INF 1e16 typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> Q; typedef vector<ll> vec; typedef vector<vec> mat; const int inf = (int)1e9; vector<int> g[200010]; ll ans[200010]; ll dp[200010]; void f(int v,int p=-1){ if(p!=-1&&g[v].size()==1){ dp[v]=1; ans[v]=1; return; } ll t1 = 0; ll t2 = 0; for(auto u:g[v]){ //cout<<u<<endl; if(u==p)continue; f(u,v); t1+=dp[u]; t2+=dp[u]*dp[u]; } ans[v] = t1*t1+2*t1-t2+1; dp[v] = t1+1; } int main(void){ int n; cin>>n; for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; u--; v--; g[u].push_back(v); g[v].push_back(u); } f(0); for(int i=0;i<n;i++){ cout<<ans[i]<<endl; } }