結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tada721tada721
提出日時 2021-02-08 08:32:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 1,560 ms
コンパイル使用メモリ 74,312 KB
実行使用メモリ 25,120 KB
最終ジャッジ日時 2023-09-18 14:04:38
合計ジャッジ時間 6,282 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,628 KB
testcase_01 AC 4 ms
9,564 KB
testcase_02 AC 4 ms
9,480 KB
testcase_03 AC 139 ms
18,520 KB
testcase_04 AC 139 ms
18,440 KB
testcase_05 AC 138 ms
18,520 KB
testcase_06 AC 141 ms
18,468 KB
testcase_07 AC 138 ms
18,536 KB
testcase_08 AC 84 ms
15,620 KB
testcase_09 AC 38 ms
12,444 KB
testcase_10 AC 41 ms
12,624 KB
testcase_11 AC 22 ms
11,176 KB
testcase_12 AC 66 ms
14,300 KB
testcase_13 AC 79 ms
15,096 KB
testcase_14 AC 82 ms
15,368 KB
testcase_15 AC 59 ms
13,844 KB
testcase_16 AC 10 ms
10,180 KB
testcase_17 AC 10 ms
10,036 KB
testcase_18 AC 124 ms
17,656 KB
testcase_19 AC 20 ms
11,156 KB
testcase_20 AC 7 ms
9,884 KB
testcase_21 AC 59 ms
13,792 KB
testcase_22 AC 50 ms
13,176 KB
testcase_23 AC 7 ms
9,792 KB
testcase_24 AC 8 ms
9,968 KB
testcase_25 AC 6 ms
9,776 KB
testcase_26 AC 7 ms
9,860 KB
testcase_27 AC 5 ms
9,596 KB
testcase_28 AC 5 ms
9,664 KB
testcase_29 AC 9 ms
9,900 KB
testcase_30 AC 8 ms
9,928 KB
testcase_31 AC 6 ms
9,812 KB
testcase_32 AC 7 ms
9,808 KB
testcase_33 AC 26 ms
13,236 KB
testcase_34 AC 130 ms
25,120 KB
testcase_35 AC 49 ms
15,976 KB
testcase_36 AC 13 ms
10,536 KB
testcase_37 AC 82 ms
17,912 KB
testcase_38 AC 77 ms
17,388 KB
testcase_39 AC 5 ms
9,476 KB
testcase_40 AC 4 ms
9,476 KB
testcase_41 AC 5 ms
9,488 KB
testcase_42 AC 4 ms
9,464 KB
testcase_43 AC 4 ms
9,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;
ll depth[100100],subtreesize[100100];
ll a[100100],b[100100];
vector<ll> edge[100100],v[100100];
void dfs(ll r,ll p,ll d){
	depth[r]=d;
	rep(i,edge[r].size())if(edge[r][i]!=p)dfs(edge[r][i],r,d+1);
	rep(i,edge[r].size())if(edge[r][i]!=p)subtreesize[r]+=subtreesize[edge[r][i]];	
	subtreesize[r]++;
}
int main(){
	ll n;
	cin>>n;
	rep(i,n-1){
		cin>>a[i]>>b[i];
		a[i]--,b[i]--;
		edge[a[i]].push_back(b[i]);
		edge[b[i]].push_back(a[i]);
	}
	/*rep(i,n){
		if(edge[i].size()==1){*/
			dfs(0,-1,0);
			/*break;
		}
	}*/	
	rep(i,n-1){
		if(depth[a[i]]>depth[b[i]]){
			v[a[i]].push_back(n-subtreesize[a[i]]);
			v[b[i]].push_back(subtreesize[a[i]]);
		}
		else{
			v[b[i]].push_back(n-subtreesize[b[i]]);
			v[a[i]].push_back(subtreesize[b[i]]);
		}
	}
	ll ans=0;
	rep(i,n){
		ans+=n;
		rep(j,v[i].size()){
			ans+=v[i][j]*(n-v[i][j]);
		}
	}
	cout<<ans<<endl;
}
0