結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 13:35:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 479 ms / 3,000 ms
コード長 1,472 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 84,892 KB
実行使用メモリ 62,844 KB
最終ジャッジ日時 2023-09-12 04:59:00
合計ジャッジ時間 9,292 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 171 ms
16,512 KB
testcase_09 AC 130 ms
13,932 KB
testcase_10 AC 250 ms
24,780 KB
testcase_11 AC 113 ms
12,876 KB
testcase_12 AC 215 ms
19,672 KB
testcase_13 AC 222 ms
19,952 KB
testcase_14 AC 213 ms
19,948 KB
testcase_15 AC 212 ms
19,960 KB
testcase_16 AC 226 ms
20,004 KB
testcase_17 AC 261 ms
22,644 KB
testcase_18 AC 351 ms
31,312 KB
testcase_19 AC 458 ms
37,620 KB
testcase_20 AC 456 ms
38,048 KB
testcase_21 AC 461 ms
37,980 KB
testcase_22 AC 479 ms
37,980 KB
testcase_23 AC 221 ms
19,936 KB
testcase_24 AC 224 ms
19,952 KB
testcase_25 AC 80 ms
20,732 KB
testcase_26 AC 283 ms
38,552 KB
testcase_27 AC 112 ms
44,824 KB
testcase_28 AC 364 ms
62,844 KB
testcase_29 AC 342 ms
50,160 KB
testcase_30 AC 360 ms
50,300 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);
	}

	using i64 = long long;

	i64 ans = 0;
	std::vector<int> x(n), root(n), size(n);
	std::vector<i64> count(n);
	std::unordered_map<int, i64> count_root; {
		std::unordered_map<int, int> map;
		auto dfs = [&](auto&& dfs, int v, int par, int xor_val) -> void {
			auto [it, _] = map.try_emplace(xor_val, -1);
			int tmp = it->second;

			if(tmp == -1) count_root[xor_val]++;
			else count[tmp]++;

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

				map[xor_val] = to;
				dfs(dfs, to, v, xor_val ^ c[id]);

				size[v] += size[to];
				ans += count[to] * (n - size[to]);
			}
			map[xor_val] = tmp;
		}; dfs(dfs, 0, -1, 0);
	}
	for(int v = 0; v < n; v++) {
		if(root[v] == -1) ans += (count_root[x[v]] - 1) * size[v];
		else ans += count[root[v]] * size[v];
	}

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