結果

問題 No.1769 Don't Stop the Game
ユーザー polylogK
提出日時 2021-11-03 08:31:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,344 ms / 3,000 ms
コード長 2,633 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 76,076 KB
最終ジャッジ日時 2025-01-25 10:45:52
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |         int n; scanf("%d", &n);
      |                ~~~~~^~~~~~~~~~
main.cpp:21:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |                 scanf("%d%d%d", &a[i], &b[i], &c[i]); a[i]--; b[i]--;
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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