結果

問題 No.1098 LCAs
ユーザー cn_449cn_449
提出日時 2020-06-26 21:54:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,731 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 118,084 KB
実行使用メモリ 24,892 KB
最終ジャッジ日時 2023-09-18 04:20:08
合計ジャッジ時間 5,694 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
10,788 KB
testcase_01 AC 7 ms
10,620 KB
testcase_02 AC 7 ms
10,788 KB
testcase_03 AC 6 ms
10,796 KB
testcase_04 AC 8 ms
10,788 KB
testcase_05 AC 6 ms
10,784 KB
testcase_06 AC 6 ms
10,788 KB
testcase_07 AC 7 ms
10,676 KB
testcase_08 AC 7 ms
10,676 KB
testcase_09 AC 6 ms
10,732 KB
testcase_10 AC 6 ms
10,604 KB
testcase_11 AC 6 ms
10,856 KB
testcase_12 AC 6 ms
10,800 KB
testcase_13 AC 6 ms
10,788 KB
testcase_14 AC 7 ms
10,848 KB
testcase_15 AC 7 ms
10,860 KB
testcase_16 AC 7 ms
10,892 KB
testcase_17 AC 8 ms
10,972 KB
testcase_18 AC 141 ms
17,224 KB
testcase_19 AC 142 ms
17,292 KB
testcase_20 AC 144 ms
17,184 KB
testcase_21 AC 140 ms
17,484 KB
testcase_22 AC 144 ms
17,224 KB
testcase_23 AC 100 ms
17,712 KB
testcase_24 AC 110 ms
17,768 KB
testcase_25 AC 101 ms
17,932 KB
testcase_26 AC 112 ms
17,928 KB
testcase_27 AC 105 ms
17,988 KB
testcase_28 AC 182 ms
24,364 KB
testcase_29 AC 198 ms
24,892 KB
testcase_30 AC 190 ms
24,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

vector<ll> sz(200010, 1), par(200010);
vector<vector<int>> graph(200010, vector<int>());

void dfs(int n, int p) {
	par[n] = p;
	for (int i : graph[n]) {
		if (i == p) continue;
		dfs(i, n);
		sz[n] += sz[i];
	}
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n;
	cin >> n;
	rep(_, n - 1) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		graph[u].pb(v); graph[v].pb(u);
	}
	dfs(0, -1);
	rep(i, n) {
		ll ans = sz[i] * 2 - 1;
		ll cnt = 0;
		for (int j : graph[i]) {
			if (j != par[i]) {
				cnt += sz[j];
				ans -= sz[j] * sz[j];
			}
		}
		cout << ans + cnt * cnt << '\n';
	}
}
0