結果

問題 No.1098 LCAs
ユーザー akun0716akun0716
提出日時 2020-06-26 22:20:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 2,074 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 83,596 KB
実行使用メモリ 27,988 KB
最終ジャッジ日時 2023-09-18 05:38:55
合計ジャッジ時間 5,365 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,708 KB
testcase_01 AC 4 ms
10,700 KB
testcase_02 AC 5 ms
10,720 KB
testcase_03 AC 4 ms
10,764 KB
testcase_04 AC 4 ms
10,916 KB
testcase_05 AC 4 ms
10,712 KB
testcase_06 AC 4 ms
10,712 KB
testcase_07 AC 4 ms
10,696 KB
testcase_08 AC 4 ms
10,708 KB
testcase_09 AC 4 ms
10,832 KB
testcase_10 AC 4 ms
10,760 KB
testcase_11 AC 4 ms
10,700 KB
testcase_12 AC 4 ms
10,824 KB
testcase_13 AC 5 ms
10,776 KB
testcase_14 AC 5 ms
10,828 KB
testcase_15 AC 4 ms
11,092 KB
testcase_16 AC 4 ms
10,756 KB
testcase_17 AC 5 ms
10,828 KB
testcase_18 AC 174 ms
20,220 KB
testcase_19 AC 165 ms
20,236 KB
testcase_20 AC 173 ms
20,416 KB
testcase_21 AC 170 ms
20,220 KB
testcase_22 AC 166 ms
20,216 KB
testcase_23 AC 105 ms
20,060 KB
testcase_24 AC 106 ms
20,048 KB
testcase_25 AC 96 ms
20,356 KB
testcase_26 AC 106 ms
20,320 KB
testcase_27 AC 101 ms
20,316 KB
testcase_28 AC 208 ms
27,216 KB
testcase_29 AC 210 ms
27,988 KB
testcase_30 AC 206 ms
26,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 100000000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;
int comb(int a, int b) {
	if (a < b)return 0;
	if (b > a - b) return comb(a, a - b);


	int ansMul = 1;
	int ansDiv = 1;
	for (int i = 0; i < b; i++)
	{
		ansMul *= (a - i);
		ansDiv *= (i + 1);
		
	}
	return ansMul / ansDiv;
}
int N;
VI G[200010];
int ans[200010];
int t[200010];//size of subtree
int dp[200010];

void dfs1(int now, int pa) {
	fore(to, G[now]) {
		if (to == pa)continue;
		dfs1(to, now);
	}
	t[now]++;
	if (pa != -1)t[pa] += t[now];
	return;
}
void dfs(int now, int pa) {
	int cnt = comb(t[now], 2) * 2 + t[now];
	fore(to, G[now]) {
		if (to == pa)continue;
		dfs(to, now);
		cnt -= dp[to];
		dp[now] += dp[to];
	}
	//cout << now << " " << cnt << endl;
	ans[now] = cnt;
	dp[now] += cnt;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	REP(i, N - 1) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs1(0, -1);
	dfs(0, -1);
	REP(i, N) cout <<ans[i] << '\n';
	return 0;
}

0