結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 222 ms
16,024 KB
testcase_09 AC 151 ms
13,456 KB
testcase_10 AC 342 ms
24,276 KB
testcase_11 AC 157 ms
12,440 KB
testcase_12 AC 287 ms
18,912 KB
testcase_13 AC 280 ms
19,092 KB
testcase_14 AC 307 ms
19,132 KB
testcase_15 AC 309 ms
19,096 KB
testcase_16 AC 300 ms
19,140 KB
testcase_17 AC 343 ms
21,996 KB
testcase_18 AC 499 ms
31,252 KB
testcase_19 AC 596 ms
38,708 KB
testcase_20 AC 620 ms
38,916 KB
testcase_21 AC 651 ms
39,780 KB
testcase_22 AC 654 ms
39,756 KB
testcase_23 AC 293 ms
19,192 KB
testcase_24 AC 298 ms
19,160 KB
testcase_25 AC 101 ms
19,952 KB
testcase_26 AC 378 ms
40,368 KB
testcase_27 AC 134 ms
37,672 KB
testcase_28 AC 449 ms
58,352 KB
testcase_29 AC 427 ms
49,008 KB
testcase_30 AC 473 ms
48,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	# Algorithm



	## 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