結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー kotatsugame
提出日時 2021-03-10 23:57:29
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 410 bytes
コンパイル時間 580 ms
コンパイル使用メモリ 70,148 KB
実行使用メモリ 17,332 KB
最終ジャッジ日時 2024-10-12 13:49:14
合計ジャッジ時間 4,216 ms
ジャッジサーバーID
(参考情報)
judge / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   20 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
vector<int>G[1<<17];
long ans;
int dfs(int u,int p)
{
	ans+=N;
	int ch=0;
	for(int v:G[u])if(v!=p)
	{
		long c=dfs(v,u);
		ans+=c*(N-c);
		ch+=c;
	}
	ans+=(long)(ch+1)*(N-ch-1);
	return ch+1;
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int a,b;cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(0,-1);
	cout<<ans<<endl;
}
0