結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 10:17:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 527 ms / 3,000 ms
コード長 1,965 bytes
コンパイル時間 924 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 58,328 KB
最終ジャッジ日時 2023-09-12 04:58:49
合計ジャッジ時間 9,338 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 162 ms
16,044 KB
testcase_09 AC 130 ms
13,452 KB
testcase_10 AC 265 ms
24,348 KB
testcase_11 AC 110 ms
12,352 KB
testcase_12 AC 225 ms
18,864 KB
testcase_13 AC 207 ms
19,148 KB
testcase_14 AC 218 ms
19,128 KB
testcase_15 AC 237 ms
19,152 KB
testcase_16 AC 225 ms
19,180 KB
testcase_17 AC 284 ms
21,980 KB
testcase_18 AC 410 ms
31,280 KB
testcase_19 AC 527 ms
38,724 KB
testcase_20 AC 503 ms
39,016 KB
testcase_21 AC 514 ms
39,736 KB
testcase_22 AC 493 ms
39,808 KB
testcase_23 AC 217 ms
19,136 KB
testcase_24 AC 210 ms
19,136 KB
testcase_25 AC 100 ms
19,896 KB
testcase_26 AC 293 ms
40,344 KB
testcase_27 AC 132 ms
37,684 KB
testcase_28 AC 360 ms
58,328 KB
testcase_29 AC 358 ms
48,936 KB
testcase_30 AC 381 ms
48,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	# Algorithm

	各頂点 v と、その親 p について次の値を求める。
	c[v] := v の部分木にあってかつ xor[p; u] がはじめて 0 になるような u の個数

	## Time Comlexity

	O(N)
*/

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <unordered_map>

int main() {
	int n; scanf("%d", &n);
	std::vector<std::vector<int>> g(n);
	std::vector<int> a(n - 1), b(n - 1), c(n - 1);
	for(int i = 0; i < n - 1; i++) {
		scanf("%d%d%d", &a[i], &b[i], &c[i]); a[i]--; b[i]--;

		g[a[i]].push_back(i);
		g[b[i]].push_back(i);
	}

	std::vector<int> x(n), size(n); {
		auto dfs = [&](auto&& dfs, int v, int par, int xor_val) -> void {
			x[v] = xor_val;
			size[v] = 1;

			for(int id: g[v]) {
				int to = v ^ a[id] ^ b[id];
				if(to == par) continue;

				dfs(dfs, to, v, xor_val ^ c[id]);
				size[v] += size[to];
			}
		}; dfs(dfs, 0, -1, 0);
	}

	using i64 = long long;

	i64 ans = 0;
	std::vector<i64> count(n);
	std::unordered_map<int, i64> count_root; {
		std::unordered_map<int, int> map;
		for(int i = 0; i < n; i++) map[x[i]] = -1;
		auto dfs = [&](auto&& dfs, int v, int par) -> void {
			if(map[x[v]] == -1) count_root[x[v]]++;
			else count[map[x[v]]]++;

			int tmp = map[x[v]];
			for(int id: g[v]) {
				int to = a[id] ^ b[id] ^ v;
				if(to == par) continue;

				map[x[v]] = to;
				dfs(dfs, to, v);

				ans += count[to] * (n - size[to]);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1);
	}
	{
		std::unordered_map<int, int> map;
		for(int i = 0; i < n; i++) map[x[i]] = -1;
		auto dfs = [&](auto&& dfs, int v, int par) -> void {
			int root = map[x[v]];

			if(root == -1) ans += (count_root[x[v]] - 1) * size[v];
			else ans += count[root] * size[v];

			int tmp = map[x[v]];
			for(int id: g[v]) {
				int to = v ^ a[id] ^ b[id];
				if(to == par) continue;

				map[x[v]] = to;
				dfs(dfs, to, v);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1);
	}

	ans = (i64)n * (n - 1) - ans;
	printf("%lld\n", ans);
}
0