結果

問題 No.2800 Game on Tree Inverse
ユーザー 👑 potato167potato167
提出日時 2024-06-27 02:05:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 735 ms / 4,000 ms
コード長 3,152 bytes
コンパイル時間 2,690 ms
コンパイル使用メモリ 219,136 KB
実行使用メモリ 151,944 KB
最終ジャッジ日時 2024-06-27 02:05:59
合計ジャッジ時間 29,592 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 492 ms
38,220 KB
testcase_04 AC 605 ms
85,228 KB
testcase_05 AC 651 ms
150,764 KB
testcase_06 AC 678 ms
151,848 KB
testcase_07 AC 632 ms
151,944 KB
testcase_08 AC 630 ms
151,812 KB
testcase_09 AC 573 ms
151,548 KB
testcase_10 AC 629 ms
85,440 KB
testcase_11 AC 558 ms
85,228 KB
testcase_12 AC 563 ms
85,352 KB
testcase_13 AC 718 ms
85,424 KB
testcase_14 AC 735 ms
150,992 KB
testcase_15 AC 554 ms
85,448 KB
testcase_16 AC 716 ms
151,040 KB
testcase_17 AC 571 ms
85,244 KB
testcase_18 AC 700 ms
85,440 KB
testcase_19 AC 596 ms
85,624 KB
testcase_20 AC 597 ms
85,228 KB
testcase_21 AC 694 ms
85,552 KB
testcase_22 AC 642 ms
151,160 KB
testcase_23 AC 560 ms
85,308 KB
testcase_24 AC 624 ms
85,228 KB
testcase_25 AC 619 ms
85,356 KB
testcase_26 AC 642 ms
85,232 KB
testcase_27 AC 535 ms
85,620 KB
testcase_28 AC 633 ms
151,160 KB
testcase_29 AC 548 ms
53,020 KB
testcase_30 AC 642 ms
85,388 KB
testcase_31 AC 601 ms
151,376 KB
testcase_32 AC 554 ms
85,356 KB
testcase_33 AC 596 ms
85,180 KB
testcase_34 AC 603 ms
151,284 KB
testcase_35 AC 591 ms
85,256 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 6 ms
5,376 KB
testcase_48 AC 5 ms
5,376 KB
testcase_49 AC 20 ms
6,200 KB
testcase_50 AC 13 ms
5,956 KB
testcase_51 AC 315 ms
45,400 KB
testcase_52 AC 27 ms
8,364 KB
testcase_53 AC 63 ms
13,608 KB
testcase_54 AC 296 ms
45,164 KB
testcase_55 AC 553 ms
83,216 KB
testcase_56 AC 413 ms
80,420 KB
testcase_57 AC 594 ms
84,508 KB
testcase_58 AC 200 ms
42,028 KB
testcase_59 AC 262 ms
43,864 KB
testcase_60 AC 168 ms
25,096 KB
testcase_61 AC 125 ms
23,756 KB
testcase_62 AC 192 ms
25,268 KB
testcase_63 AC 215 ms
42,608 KB
testcase_64 AC 77 ms
13,984 KB
testcase_65 AC 76 ms
14,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;


struct node{
	int sum = 0;
	int depth = - 1;
	array<int, 2> to = {-1, -1};
};

int f(int a,int d){
	return (a & (1 << d)) != 0;
}

vector<node> nodes;

struct binary{
	int root;
	int C = 0;
	int D = 1;
	binary(){
		root = nodes.size();
		node tmp;
		tmp.depth = 0;
		nodes.push_back(tmp);
	}
	bool get(int a){
		a ^= C;
		while (a < (1 << D)){
			node tmp;
			tmp.depth = D;
			tmp.to[0] = root;
			root = nodes.size();
			nodes.push_back(tmp);
			D++;
		}
		int ind = root;
		while (nodes[ind].depth != -1){
			int b = f(a, nodes[ind].depth);
			if (nodes[ind].to[b] == -1) return false;
			ind = nodes[ind].to[b];
		}
		return true;
	}
	void set(int a){
		if (get(a)) return;
		a ^= C;
		int ind = root;
		while (nodes[ind].depth != -1){
			int b = f(a, nodes[ind].depth);
			if (nodes[ind].to[b] == -1){
				nodes[ind].to[b] = nodes.size();
				node tmp;
				tmp.depth = nodes[ind].depth - 1;
				nodes.push_back(tmp);
			}
			ind = nodes[ind].to[b];
			nodes[ind].sum++;
		}
	}
	int mim(){
		int ans = 0;
		int ind = root;
		if (nodes[root].sum == (1 << D)) return (1 << D);
		while (ind != -1 && nodes[ind].depth != -1){
			int b = f(C, nodes[ind].depth);
			int d = nodes[ind].to[b];
			if (d != -1 && nodes[d].sum == (1 << nodes[ind].depth)){
				ans += nodes[d].sum;
				ind = nodes[ind].to[1 - b];
			}
			else{
				ind = d;
			}
		}
		return ans;
	}
	vector<int> get_list(){
		vector<int> res;
		auto f = [&](auto self, int ind, int val) -> void {
			if (ind == -1) return;
			if (nodes[ind].depth == -1){
				res.push_back(val ^ C);
				return;
			}
			for (int i = 0; i < 2; i++){
				self(self, nodes[ind].to[i], val + i * (1 << nodes[ind].depth));
			}
		};
		f(f, root, 0);
		return res;
		
	}
};

int main(){
	int N;
	cin >> N;
	vector<vector<int>> G(N);
	for (int i = 0; i < N - 1; i++){
		int a, b;
		cin >> a >> b;
		a--, b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	vector<int> order = {0}, pare(N, -1);
	pare[0] = -2;
	for (int i = 0; i < N; i++){
		int a = order[i];
		for (auto x : G[a]) if (pare[x] == -1){
			order.push_back(x);
			pare[x] = a;
		}
	}
	vector<binary> dp(N);
	vector<int> val(N), ans, wei(N, 1);
	for (int i = N - 1; i >= 0; i--){
		int a = order[i];
		int ind = -1;
		int sum = 0;
		for (auto x : G[a]) if (pare[x] == a){
			sum ^= val[x];
			wei[a] += wei[x];
			if (ind == -1 || wei[x] > wei[ind]) ind = x;
		}
		if (ind != -1){
			dp[ind].C ^= (sum ^ val[ind]);
			swap(dp[ind], dp[a]);
		}
		for (auto x : G[a]) if (pare[x] == a && x != ind){
			dp[x].C ^= (sum ^ val[x]);
			for (auto y : dp[x].get_list()){
				dp[a].set(y);
			}
		}
		dp[a].set(sum);
		val[a] = dp[a].mim();
	}
	auto dfs = [&](auto self, int ind, int v) -> void {
		for (auto x : G[ind]) if (pare[x] == ind){
			v ^= val[x];
		}
		if (v == 0) ans.push_back(ind + 1);
		for (auto x : G[ind]) if (pare[x] == ind){
			self(self, x, v ^ val[x]);
		}
	};
	dfs(dfs, 0, 0);
	cout << "Alice\n";
	cout << ans.size() << "\n";
	sort(ans.begin(), ans.end());
	for (int i = 0; i < (int)ans.size(); i++){
		if (i) cout << " ";
		cout << ans[i];
	}
	cout << endl;
	return 0;
}
0