結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー |
![]() |
提出日時 | 2021-03-05 21:38:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 873 bytes |
コンパイル時間 | 1,558 ms |
コンパイル使用メモリ | 172,828 KB |
実行使用メモリ | 218,236 KB |
最終ジャッジ日時 | 2024-10-07 01:01:13 |
合計ジャッジ時間 | 5,368 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | TLE * 1 -- * 40 |
ソースコード
//g++ 5.cpp -std=c++14 -I .#include <bits/stdc++.h>using namespace std;using ll = long long;using ld = long double;#define fi first#define se second#define pb push_backint x[100500];int dist[100500];int seen[100500]={};void dfs(vector<vector<int>> g,int now){seen[now]=1;int z=0;for(int next:g[now]){if(seen[next]==0){dist[next]=dist[now]+1;dfs(g,next);z+=x[next];}}x[now]=z+1;}int main(){int n;cin>>n;vector<vector<int>> g(n);int v[n-1][2];for(int i=0;i<n-1;i++){int a,b;cin>>a>>b;a--;b--;g[a].pb(b);g[b].pb(a);v[i][0]=a,v[i][1]=b;}dist[0]=0;dfs(g,0);ll ans=0;for(int i=0;i<n-1;i++){int a=v[i][0],b=v[i][1];if(dist[a]>dist[b]){swap(a,b);}ans+=((ll)x[b])*(n-x[b]);}ans*=2;ans+=(ll)n*n;cout<<ans<<endl;}