結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 14:10:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 675 ms / 3,000 ms
コード長 1,514 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 80,896 KB
実行使用メモリ 59,544 KB
最終ジャッジ日時 2023-09-12 05:00:40
合計ジャッジ時間 10,899 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 176 ms
16,028 KB
testcase_09 AC 189 ms
13,728 KB
testcase_10 AC 340 ms
23,980 KB
testcase_11 AC 128 ms
12,296 KB
testcase_12 AC 165 ms
18,952 KB
testcase_13 AC 175 ms
19,152 KB
testcase_14 AC 167 ms
19,144 KB
testcase_15 AC 186 ms
19,080 KB
testcase_16 AC 237 ms
19,460 KB
testcase_17 AC 385 ms
22,516 KB
testcase_18 AC 552 ms
32,416 KB
testcase_19 AC 629 ms
37,160 KB
testcase_20 AC 675 ms
38,056 KB
testcase_21 AC 643 ms
37,940 KB
testcase_22 AC 612 ms
37,968 KB
testcase_23 AC 161 ms
19,212 KB
testcase_24 AC 170 ms
19,108 KB
testcase_25 AC 79 ms
19,924 KB
testcase_26 AC 494 ms
38,736 KB
testcase_27 AC 113 ms
40,784 KB
testcase_28 AC 575 ms
59,544 KB
testcase_29 AC 513 ms
48,840 KB
testcase_30 AC 550 ms
48,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	# Algorithm

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

	## Time Comlexity

	O(N)
*/

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <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 = (i64)n * (n - 1);
	std::vector<int> x(n), root(n), size(n), count(n);
	std::map<int, int> count_root; {
		std::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 -= (i64)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 -= (i64)(count_root[x[v]] - 1) * size[v];
		else ans -= (i64)count[root[v]] * size[v];
	}
	printf("%lld\n", ans);
}
0