結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー publflpublfl
提出日時 2021-03-05 21:47:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 722 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 49,792 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-04-16 09:13:18
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,144 KB
testcase_01 AC 4 ms
6,016 KB
testcase_02 AC 4 ms
6,016 KB
testcase_03 AC 66 ms
9,600 KB
testcase_04 AC 66 ms
9,600 KB
testcase_05 AC 64 ms
9,728 KB
testcase_06 AC 69 ms
9,600 KB
testcase_07 AC 63 ms
9,472 KB
testcase_08 AC 42 ms
8,576 KB
testcase_09 AC 20 ms
7,040 KB
testcase_10 AC 20 ms
7,168 KB
testcase_11 AC 11 ms
6,784 KB
testcase_12 AC 33 ms
7,936 KB
testcase_13 AC 34 ms
8,320 KB
testcase_14 AC 41 ms
8,320 KB
testcase_15 AC 28 ms
7,936 KB
testcase_16 AC 7 ms
6,272 KB
testcase_17 AC 6 ms
6,272 KB
testcase_18 AC 60 ms
9,216 KB
testcase_19 AC 11 ms
6,784 KB
testcase_20 AC 5 ms
6,144 KB
testcase_21 AC 28 ms
7,680 KB
testcase_22 AC 24 ms
7,552 KB
testcase_23 AC 5 ms
6,144 KB
testcase_24 AC 5 ms
6,272 KB
testcase_25 AC 5 ms
6,272 KB
testcase_26 AC 4 ms
6,272 KB
testcase_27 AC 4 ms
6,272 KB
testcase_28 AC 4 ms
6,272 KB
testcase_29 AC 5 ms
6,400 KB
testcase_30 AC 4 ms
6,272 KB
testcase_31 AC 4 ms
6,272 KB
testcase_32 AC 5 ms
6,144 KB
testcase_33 AC 15 ms
8,704 KB
testcase_34 AC 67 ms
17,152 KB
testcase_35 AC 24 ms
10,752 KB
testcase_36 AC 8 ms
6,528 KB
testcase_37 AC 36 ms
9,380 KB
testcase_38 AC 34 ms
9,172 KB
testcase_39 AC 4 ms
6,144 KB
testcase_40 AC 4 ms
5,888 KB
testcase_41 AC 4 ms
6,016 KB
testcase_42 AC 4 ms
6,144 KB
testcase_43 AC 3 ms
5,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>

int a;
long long int ans = 0;
std::vector<int> V[100010];

int check[100010];
int getSize(int k, int prev)
{
	if(check[k]) return check[k];
	int ans = 1;
	for(int i=0;i<V[k].size();i++)
	{
		if(V[k][i]==prev) continue;
		ans += getSize(V[k][i],k);
	}
	return check[k] = ans;
}

void func(int k, int prev)
{
	ans += 1LL * (a-getSize(k,prev))*getSize(k,prev);
	ans += a;
	for(int i=0;i<V[k].size();i++)
	{
		if(V[k][i]==prev) continue;
		int t = getSize(V[k][i],k);
		ans += 1LL * (a-t) * t;
		func(V[k][i],k);
	}
}
int main()
{
	scanf("%d",&a);
	for(int i=1;i<a;i++)
	{
		int b,c;
		scanf("%d%d",&b,&c);
		V[b].push_back(c);
		V[c].push_back(b);
	}
	func(1,0);
	printf("%lld\n",ans);
}
0