結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 08:46:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,646 ms / 3,000 ms
コード長 2,345 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 82,028 KB
実行使用メモリ 112,012 KB
最終ジャッジ日時 2023-09-12 04:56:41
合計ジャッジ時間 22,114 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 365 ms
31,732 KB
testcase_09 AC 423 ms
26,476 KB
testcase_10 AC 910 ms
49,184 KB
testcase_11 AC 277 ms
23,824 KB
testcase_12 AC 306 ms
37,652 KB
testcase_13 AC 320 ms
38,368 KB
testcase_14 AC 332 ms
38,432 KB
testcase_15 AC 332 ms
38,432 KB
testcase_16 AC 525 ms
38,744 KB
testcase_17 AC 953 ms
45,280 KB
testcase_18 AC 1,375 ms
67,896 KB
testcase_19 AC 1,511 ms
79,252 KB
testcase_20 AC 1,603 ms
81,064 KB
testcase_21 AC 1,646 ms
81,148 KB
testcase_22 AC 1,610 ms
81,252 KB
testcase_23 AC 274 ms
38,308 KB
testcase_24 AC 305 ms
38,304 KB
testcase_25 AC 115 ms
35,448 KB
testcase_26 AC 1,276 ms
77,024 KB
testcase_27 AC 227 ms
79,420 KB
testcase_28 AC 1,638 ms
112,012 KB
testcase_29 AC 1,516 ms
93,168 KB
testcase_30 AC 1,481 ms
93,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	# Algorithm



	## Time Comlexity

	O(Nlog(N))
*/

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

	std::vector<int> x(n), y(n), size(n), in(n), out(n), tour;
	std::vector<std::vector<int>> child(n), child_order(n); // LA
	std::map<int, std::vector<std::vector<int>>> map_xor; {
		std::map<int, int> map;
		auto dfs = [&](auto&& dfs, int v, int par, int xor_val) -> void {
			in[v] = tour.size();
			tour.push_back(v);

			x[v] = xor_val;
			y[v] = map[xor_val]++;
			auto& vec = map_xor[xor_val];
			if(vec.size() <= y[v]) vec.resize(y[v] + 2);
			vec[y[v]].push_back(in[v]);

			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]);
				child[v].push_back(to);
				child_order[v].push_back(in[to]);
				size[v] += size[to];
			}

			tour.push_back(v);
			out[v] = tour.size();

			map[xor_val]--;
		}; dfs(dfs, 0, -1, 0);
	}

	using i64 = long long;
	i64 ans = 0;
	{ // 解説のパターン 1, 2 を数える
		std::map<int, int> map;
		auto dfs = [&](auto&& dfs, int v, int par, int depth) -> void {
			if(y[v]) ans += map[x[v]] + size[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]] = n - size[to];
				dfs(dfs, to, v, depth + 1);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1, 0);
	}
	{ // 解説のパターン 3 を数える
		std::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(v and map[x[v]] != v) {
				int root = (y[v] ? map[x[v]] : 0);
				auto& vec = map_xor[x[v]][y[v]];

				i64 cnt = std::distance(lower_bound(begin(vec), end(vec), in[root]),
						 				lower_bound(begin(vec), end(vec), out[root])) - 1;
				ans += cnt * size[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);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1);
	}

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