結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー KKT89KKT89
提出日時 2021-03-05 21:47:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 808 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 203,576 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-04-16 09:12:38
合計ジャッジ時間 4,523 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 57 ms
9,984 KB
testcase_04 AC 56 ms
9,856 KB
testcase_05 AC 58 ms
9,856 KB
testcase_06 AC 57 ms
9,728 KB
testcase_07 AC 67 ms
9,856 KB
testcase_08 AC 35 ms
7,680 KB
testcase_09 AC 16 ms
5,376 KB
testcase_10 AC 17 ms
5,760 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 29 ms
6,784 KB
testcase_13 AC 36 ms
7,168 KB
testcase_14 AC 36 ms
7,424 KB
testcase_15 AC 24 ms
6,528 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 50 ms
9,088 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 24 ms
6,528 KB
testcase_22 AC 20 ms
6,144 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 11 ms
5,632 KB
testcase_34 AC 61 ms
13,568 KB
testcase_35 AC 21 ms
7,808 KB
testcase_36 AC 5 ms
5,376 KB
testcase_37 AC 34 ms
9,724 KB
testcase_38 AC 30 ms
9,296 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

constexpr ll mod=1e9+7;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<vector<int>> g(n);
	for(int i=1;i<n;i++){
		int x,y; cin >> x >> y;
		x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	ll res=0;
	vector<ll> d(n);
	auto dfs=[&](auto dfs,int s,int p)->void{
		d[s]=1;
		for(int t:g[s]){
			if(t==p)continue;
			dfs(dfs,t,s);
			d[s]+=d[t];
		}
	};
	dfs(dfs,0,-1);
	res+=(ll)n*(ll)n;
	for(int i=0;i<n;i++){
		for(int t:g[i]){
			if(d[t]>d[i])continue;
			res+=d[t]*(n-d[t]);
		}
		res+=(ll)(n-d[i])*d[i];
	}
	printf("%lld\n",res);
}
0