結果

問題 No.1769 Don't Stop the Game
ユーザー polylogK
提出日時 2021-11-03 09:31:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,124 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 78,716 KB
最終ジャッジ日時 2025-01-25 10:47:16
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘main()::<lambda(auto:2&&, int, int)> [with auto:2 = main()::<lambda(auto:2&&, int, int)>&]’:
main.cpp:70:9:   required from here
main.cpp:67:48: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘i64’ {aka ‘long long int’} [-Wformat=]
   67 |                                 printf("A: %d %d %d\n", v + 1, diff, n - size[to]);
      |                                               ~^               ~~~~
      |                                                |               |
      |                                                int             i64 {aka long long int}
      |                                               %lld
main.cpp: In instantiation of ‘main()::<lambda(auto:3&&, int, int)> [with auto:3 = main()::<lambda(auto:3&&, int, int)>&]’:
main.cpp:94:9:   required from here
main.cpp:79:48: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘std::unordered_map<int, long long int>::mapped_type’ {aka ‘long long int’} [-Wformat=]
   79 |                                 printf("B: %d %d %d\n", v + 1, count_root[x[v]] - 1, size[v]);
      |                                               ~^
      |                                                |
      |                                                int
      |                                               %lld
main.cpp:82:48: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type’ {aka ‘long long int’} [-Wformat=]
   82 |                                 printf("B: %d %d %d\n", v + 1, count[root], size[v]);
      |                                               ~^
      |                                                |
      |                                                int
      |                                               %lld
main.cpp: In function ‘int main()’:
m

ソースコード

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