結果

問題 No.1098 LCAs
ユーザー Y17Y17
提出日時 2020-06-26 22:50:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 528 ms / 2,000 ms
コード長 752 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 166,764 KB
実行使用メモリ 32,144 KB
最終ジャッジ日時 2023-09-18 06:45:52
合計ジャッジ時間 9,704 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,076 KB
testcase_01 AC 4 ms
9,080 KB
testcase_02 AC 4 ms
9,136 KB
testcase_03 AC 4 ms
9,204 KB
testcase_04 AC 4 ms
9,096 KB
testcase_05 AC 4 ms
9,060 KB
testcase_06 AC 4 ms
9,096 KB
testcase_07 AC 4 ms
9,256 KB
testcase_08 AC 4 ms
9,136 KB
testcase_09 AC 5 ms
9,044 KB
testcase_10 AC 4 ms
9,060 KB
testcase_11 AC 4 ms
9,080 KB
testcase_12 AC 4 ms
9,068 KB
testcase_13 AC 6 ms
9,116 KB
testcase_14 AC 6 ms
9,200 KB
testcase_15 AC 6 ms
9,228 KB
testcase_16 AC 6 ms
9,260 KB
testcase_17 AC 6 ms
9,256 KB
testcase_18 AC 488 ms
18,768 KB
testcase_19 AC 480 ms
18,568 KB
testcase_20 AC 492 ms
18,580 KB
testcase_21 AC 498 ms
18,868 KB
testcase_22 AC 495 ms
18,696 KB
testcase_23 AC 420 ms
18,720 KB
testcase_24 AC 418 ms
18,672 KB
testcase_25 AC 399 ms
18,492 KB
testcase_26 AC 440 ms
18,444 KB
testcase_27 AC 435 ms
18,504 KB
testcase_28 AC 520 ms
30,712 KB
testcase_29 AC 528 ms
32,144 KB
testcase_30 AC 524 ms
30,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int n;
vector<int> tree[200010];

int m1[200010];
int m2[200010];

void dp(int idx, int par){
	int ans = 1;
	int sub = 1;

	for(auto e : tree[idx]){
		if(e != par){
			dp(e, idx);
			ans += sub * m2[e] * 2;
			sub += m2[e];
		}
	}

	m1[idx] = ans;
	m2[idx] = sub;
	return;
}

signed main(){
	cin >> n;
	for(int i = 0;i < n-1;i++){
		int u, v;
		cin >> u >> v;
		u--; v--;
		tree[u].push_back(v);
		tree[v].push_back(u);
	}

	dp(0, -1);

	for(int i = 0;i < n;i++){
		cout << m1[i] << endl;
	}

	return 0;
}
0