結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-06 13:45:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 456 ms / 3,000 ms
コード長 1,235 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 78,844 KB
実行使用メモリ 62,084 KB
最終ジャッジ日時 2023-09-12 05:01:15
合計ジャッジ時間 8,085 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 146 ms
15,988 KB
testcase_09 AC 97 ms
13,472 KB
testcase_10 AC 188 ms
24,164 KB
testcase_11 AC 92 ms
12,420 KB
testcase_12 AC 168 ms
18,880 KB
testcase_13 AC 162 ms
19,200 KB
testcase_14 AC 163 ms
19,224 KB
testcase_15 AC 163 ms
19,156 KB
testcase_16 AC 175 ms
19,204 KB
testcase_17 AC 221 ms
21,880 KB
testcase_18 AC 296 ms
30,644 KB
testcase_19 AC 327 ms
36,924 KB
testcase_20 AC 365 ms
37,256 KB
testcase_21 AC 412 ms
37,208 KB
testcase_22 AC 456 ms
37,284 KB
testcase_23 AC 213 ms
19,204 KB
testcase_24 AC 211 ms
19,152 KB
testcase_25 AC 79 ms
19,924 KB
testcase_26 AC 297 ms
37,816 KB
testcase_27 AC 109 ms
43,996 KB
testcase_28 AC 374 ms
62,084 KB
testcase_29 AC 353 ms
49,428 KB
testcase_30 AC 369 ms
49,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = (i64)n * (n - 1);
	std::vector<int> x(n), root(n), size(n), count(n);
	std::unordered_map<int, int> 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 -= (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