結果
問題 |
No.1418 Sum of Sum of Subtree Size
|
ユーザー |
|
提出日時 | 2021-07-29 03:20:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 99 ms / 2,000 ms |
コード長 | 564 bytes |
コンパイル時間 | 1,708 ms |
コンパイル使用メモリ | 171,408 KB |
実行使用メモリ | 17,920 KB |
最終ジャッジ日時 | 2024-09-14 01:46:42 |
合計ジャッジ時間 | 4,783 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#include<bits/stdc++.h> using namespace std; int main(){ long n,u,v,ans=0; cin>>n; vector<vector<int>> g(n); for(int i=0;i<n-1;i++){ cin>>u>>v; g[--u].push_back(--v); g[v].push_back(u); } function<int(int,int)> dfs=[&](int u,int p){ long ret=1,d; ans+=n; for(auto v:g[u]){ if(v!=p){ d=dfs(v,u); ret+=d; ans+=d*(n-d); } } ans+=ret*(n-ret); return ret; }; dfs(0,-1); cout<<ans<<endl; }