結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 09:31:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,124 bytes
コンパイル時間 1,068 ms
コンパイル使用メモリ 83,692 KB
実行使用メモリ 61,812 KB
最終ジャッジ日時 2023-09-12 04:58:37
合計ジャッジ時間 15,506 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	}

	for(auto v: x) printf("%d ", v);
	printf("\n");

	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[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 prev = count[v];
			int tmp = map[x[v]]; map[x[v]] = v;
			for(int id: g[v]) {
				int to = a[id] ^ b[id] ^ v;
				if(to == par) continue;

				dfs(dfs, to, v);

				i64 diff = count[v] - prev; prev = count[v];
				ans += diff * (n - size[to]);

				printf("A: %d %d %d\n", v + 1, diff, 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[i] = -1;
		auto dfs = [&](auto&& dfs, int v, int par) -> void {
			int root = map[x[v]];

			if(root == -1) {
				printf("B: %d %d %d\n", v + 1, count_root[x[v]] - 1, size[v]);
				ans += (count_root[x[v]] - 1) * size[v];
			} else {
				printf("B: %d %d %d\n", v + 1, count[root], size[v]);
				ans += count[root] * size[v];
			}

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

				dfs(dfs, to, v);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1);
	}
	printf("%lld\n", ans);

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