結果

問題 No.1769 Don't Stop the Game
ユーザー polylogKpolylogK
提出日時 2021-11-03 08:31:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,650 ms / 3,000 ms
コード長 2,633 bytes
コンパイル時間 1,349 ms
コンパイル使用メモリ 81,740 KB
実行使用メモリ 112,012 KB
最終ジャッジ日時 2023-09-12 04:55:27
合計ジャッジ時間 22,480 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 376 ms
31,688 KB
testcase_09 AC 420 ms
26,532 KB
testcase_10 AC 937 ms
49,096 KB
testcase_11 AC 277 ms
23,848 KB
testcase_12 AC 332 ms
37,732 KB
testcase_13 AC 325 ms
38,392 KB
testcase_14 AC 339 ms
38,420 KB
testcase_15 AC 365 ms
38,456 KB
testcase_16 AC 510 ms
38,772 KB
testcase_17 AC 923 ms
45,300 KB
testcase_18 AC 1,411 ms
67,908 KB
testcase_19 AC 1,521 ms
79,316 KB
testcase_20 AC 1,559 ms
81,120 KB
testcase_21 AC 1,605 ms
81,240 KB
testcase_22 AC 1,650 ms
81,268 KB
testcase_23 AC 294 ms
38,296 KB
testcase_24 AC 368 ms
38,368 KB
testcase_25 AC 116 ms
35,480 KB
testcase_26 AC 1,374 ms
76,928 KB
testcase_27 AC 234 ms
79,396 KB
testcase_28 AC 1,644 ms
112,012 KB
testcase_29 AC 1,494 ms
93,200 KB
testcase_30 AC 1,543 ms
93,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	# Algorithm



	## Time Comlexity

	O(Nlog(N)^2)
*/

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

	auto get_child = [&](int v, int id) -> int { // 頂点 v の子であって id が部分木に含まれるようなものを返す
		int k = std::prev(upper_bound(begin(child_order[v]), end(child_order[v]), id))
			  	- begin(child_order[v]);
		return child[v][k];
	};

	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]] != par) {
				int root = (y[v] ? get_child(map[x[v]], in[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]]; map[x[v]] = v;
			for(int id: g[v]) {
				int to = a[id] ^ b[id] ^ v;
				if(to == par) continue;

				dfs(dfs, to, v);
			}
			map[x[v]] = tmp;
		}; dfs(dfs, 0, -1);
	}

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