結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-16 12:45:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 126,292 KB
実行使用メモリ 16,880 KB
最終ジャッジ日時 2023-10-16 12:45:21
合計ジャッジ時間 6,284 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,508 KB
testcase_01 AC 3 ms
6,708 KB
testcase_02 AC 3 ms
6,432 KB
testcase_03 AC 62 ms
10,840 KB
testcase_04 AC 61 ms
11,060 KB
testcase_05 AC 66 ms
10,800 KB
testcase_06 AC 60 ms
10,852 KB
testcase_07 AC 66 ms
10,804 KB
testcase_08 AC 39 ms
9,424 KB
testcase_09 AC 17 ms
7,828 KB
testcase_10 AC 19 ms
8,004 KB
testcase_11 AC 11 ms
7,272 KB
testcase_12 AC 29 ms
8,784 KB
testcase_13 AC 33 ms
9,192 KB
testcase_14 AC 36 ms
9,380 KB
testcase_15 AC 25 ms
8,580 KB
testcase_16 AC 6 ms
6,712 KB
testcase_17 AC 5 ms
6,784 KB
testcase_18 AC 62 ms
10,556 KB
testcase_19 AC 10 ms
7,192 KB
testcase_20 AC 4 ms
6,560 KB
testcase_21 AC 26 ms
8,532 KB
testcase_22 AC 24 ms
8,452 KB
testcase_23 AC 4 ms
6,660 KB
testcase_24 AC 4 ms
6,840 KB
testcase_25 AC 4 ms
6,552 KB
testcase_26 AC 4 ms
6,596 KB
testcase_27 AC 3 ms
6,460 KB
testcase_28 AC 3 ms
6,536 KB
testcase_29 AC 5 ms
6,704 KB
testcase_30 AC 5 ms
6,624 KB
testcase_31 AC 4 ms
6,524 KB
testcase_32 AC 4 ms
6,568 KB
testcase_33 AC 13 ms
9,056 KB
testcase_34 AC 66 ms
16,880 KB
testcase_35 AC 23 ms
10,816 KB
testcase_36 AC 7 ms
7,020 KB
testcase_37 AC 33 ms
10,460 KB
testcase_38 AC 31 ms
10,164 KB
testcase_39 AC 3 ms
6,444 KB
testcase_40 AC 3 ms
6,708 KB
testcase_41 AC 3 ms
6,552 KB
testcase_42 AC 3 ms
6,424 KB
testcase_43 AC 3 ms
6,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
using namespace std;
int N,ch[1 << 17];
vector<int> G[1 << 17];
long long dp[1 << 17];
void dfs0(int u,int p)
{
	ch[u] = 1;
	for(int v:G[u]) if(v != p)
	{
		dfs0(v,u);
		ch[u] += ch[v];
		dp[u] += dp[v];
	}
	dp[u] += ch[u];
}
void dfs1(int u,int p)
{
	if(p >= 0)
	{
		long long pdp = dp[p];
		pdp -= dp[u]+ch[u];
		dp[u] += pdp+N-ch[u];
	}
	for(int v:G[u]) if(v != p) dfs1(v,u);
}
void solve()
{
	cin >> N;
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs0(0,-1);
	dfs1(0,-1);
	long long ans = 0;
	for(int i = 0;i < N;i++) ans += dp[i];
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0