結果

問題 No.1098 LCAs
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-09-21 21:52:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 167,008 KB
実行使用メモリ 31,936 KB
最終ジャッジ日時 2023-09-07 01:55:10
合計ジャッジ時間 11,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,104 KB
testcase_01 AC 3 ms
9,064 KB
testcase_02 AC 4 ms
9,092 KB
testcase_03 AC 4 ms
9,100 KB
testcase_04 AC 4 ms
9,156 KB
testcase_05 AC 4 ms
9,064 KB
testcase_06 AC 3 ms
9,196 KB
testcase_07 AC 4 ms
9,128 KB
testcase_08 AC 3 ms
9,116 KB
testcase_09 AC 4 ms
9,060 KB
testcase_10 AC 5 ms
9,108 KB
testcase_11 AC 4 ms
9,064 KB
testcase_12 AC 4 ms
9,212 KB
testcase_13 AC 5 ms
9,252 KB
testcase_14 AC 6 ms
9,364 KB
testcase_15 AC 6 ms
9,112 KB
testcase_16 AC 5 ms
9,144 KB
testcase_17 AC 6 ms
9,152 KB
testcase_18 AC 445 ms
17,600 KB
testcase_19 AC 438 ms
17,708 KB
testcase_20 AC 443 ms
17,564 KB
testcase_21 AC 451 ms
17,632 KB
testcase_22 AC 434 ms
17,588 KB
testcase_23 AC 392 ms
17,784 KB
testcase_24 AC 386 ms
17,816 KB
testcase_25 AC 367 ms
17,852 KB
testcase_26 AC 407 ms
17,732 KB
testcase_27 AC 400 ms
17,744 KB
testcase_28 AC 475 ms
30,848 KB
testcase_29 AC 475 ms
31,936 KB
testcase_30 AC 484 ms
30,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
typedef vector<ll> vec;
typedef vector<vec> mat;
const int inf = (int)1e9; 
 
vector<int> g[200010];
ll ans[200010];
ll dp[200010];

void f(int v,int p=-1){
	if(p!=-1&&g[v].size()==1){
		dp[v]=1;
		ans[v]=1;
		return;
	}
	ll t1 = 0;
	ll t2 = 0;

	for(auto u:g[v]){
		//cout<<u<<endl;
		if(u==p)continue;
		f(u,v);
		t1+=dp[u];
		t2+=dp[u]*dp[u];
	}
	ans[v] = t1*t1+2*t1-t2+1;
	dp[v] = t1+1;
}

int main(void){
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		u--;
		v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	f(0);
	for(int i=0;i<n;i++){
		cout<<ans[i]<<endl;
	}
}
0