結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tkmst201tkmst201
提出日時 2021-05-23 09:47:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,212 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 199,828 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2024-04-19 20:28:29
合計ジャッジ時間 4,859 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 54 ms
9,344 KB
testcase_04 AC 49 ms
9,344 KB
testcase_05 AC 51 ms
9,344 KB
testcase_06 AC 48 ms
9,344 KB
testcase_07 AC 46 ms
9,344 KB
testcase_08 AC 31 ms
7,296 KB
testcase_09 AC 15 ms
5,504 KB
testcase_10 AC 17 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 26 ms
6,656 KB
testcase_13 AC 30 ms
7,040 KB
testcase_14 AC 30 ms
7,424 KB
testcase_15 AC 24 ms
6,400 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 44 ms
8,832 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 23 ms
6,400 KB
testcase_22 AC 20 ms
5,888 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 12 ms
8,448 KB
testcase_34 AC 55 ms
26,112 KB
testcase_35 AC 25 ms
13,056 KB
testcase_36 AC 5 ms
5,376 KB
testcase_37 AC 35 ms
9,960 KB
testcase_38 AC 32 ms
9,660 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

int main() {
	int N;
	cin >> N;
	vector<vector<int>> g(N);
	REP(i, N - 1) {
		int a, b;
		scanf("%d %d", &a, &b);
		--a; --b;
		g[a].emplace_back(b);
		g[b].emplace_back(a);
	}
	
	ll ans = 0;
	vector<int> childs(N);
	auto dfs = [&](auto self, int u, int p) -> int {
		bool leaf = true;
		vector<int> childs;
		int res = 1;
		for (int v : g[u]) if (v != p) {
			leaf = false;
			childs.emplace_back(self(self, v, u));
			res += childs.back();
		}
		childs.emplace_back(N - res);
		for (int c : childs) ans += (ll)c * (N - c);
		return res;
	};
	dfs(dfs, 0, -1);
	
	ans += (ll)N * N;
	cout << ans << endl;
}
0