結果

問題 No.2800 Game on Tree Inverse
ユーザー shobonvip
提出日時 2024-06-27 01:18:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,669 ms / 4,000 ms
コード長 2,834 bytes
コンパイル時間 471 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 303,348 KB
最終ジャッジ日時 2024-06-27 01:22:59
合計ジャッジ時間 82,434 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 64
権限があれば一括ダウンロードができます

ソースコード

diff #

class binary_trie:
	def __init__(self, MAX_LOG):
		self.ch = [-1] * 2
		self.cnt = [0]
		self.id = 1
		self.xor_all = 0
		self.max_log = MAX_LOG

	def insert(self, val):
		val ^= self.xor_all
		node = 0
		for b in range(self.max_log-1, -1, -1):
			f = val >> b & 1
			self.cnt[node] += 1
			if self.ch[(node<<1)|f] == -1:
				self.ch[(node<<1)|f] = self.id
				self.ch.append(-1)
				self.ch.append(-1)
				self.cnt.append(0)
				node = self.id
				self.id += 1
			else:
				node = self.ch[(node<<1)|f]
		self.cnt[node] += 1
	
	def size(self, node):
		if node == -1: return 0
		return self.cnt[node]
	
	def count(self, val):
		val ^= self.xor_all
		node = 0
		for b in range(self.max_log-1, -1, -1):
			if node == -1: break
			node = self.ch[(node<<1)|(val>>b&1)]
		return self.size(node)

	def xth(self, x):
		node = 0
		ret = 0
		for b in range(self.max_log-1, -1, -1):
			f = self.xor_all>>b&1
			if self.size(self.ch[(node<<1)|f]) <= x:
				x -= self.size(self.ch[(node<<1)|f])
				node = self.ch[(node<<1)|(f^1)]
				ret = (ret<<1)|(f^1)
			else:
				node = self.ch[(node<<1)|f]
				ret = (ret<<1)|f
		return ret ^ self.xor_all
	
	def mex(self):
		ub = self.cnt[0]
		lb = -1
		while ub - lb > 1:
			t = (ub + lb) // 2
			if self.xth(t) == t: lb = t
			else: ub = t
		return lb + 1

	def xor_to_all(self, x):
		self.xor_all ^= x
		return

n = int(input())
ikeru = [[] for i in range(n)]
for i in range(n-1):
	u, v = map(int,input().split())
	u -= 1
	v -= 1
	ikeru[u].append(v)
	ikeru[v].append(u)

dp = [0] * n
ep = [binary_trie(18) for i in range(n)]
re = [i for i in range(n)]

mada = [~0, 0]
tansaku = [0] * n
tansaku[0] = 1
while mada:
	i = mada.pop()
	if i >= 0:
		for j in ikeru[i]:
			if tansaku[j] == 1: continue
			tansaku[j] = 1
			mada.append(~j)
			mada.append(j)
	else:
		i = ~i
		x = 0
		mxval = 0
		mxind = i
		for j in ikeru[i]:
			if tansaku[j] == 2:
				x ^= dp[j]
				if mxval < ep[re[j]].cnt[0]:
					mxval = ep[re[j]].cnt[0]
					mxind = j
		
		ep[re[mxind]].xor_to_all(dp[mxind]^x)

		for j in ikeru[i]:
			if tansaku[j] == 2:
				if j == mxind:
					continue
				mxz = ep[re[j]].cnt[0]
				for z in range(mxz):
					tar = ep[re[j]].xth(z)
					if ep[re[mxind]].count(tar^x^dp[j]) == 0:
						ep[re[mxind]].insert(tar^x^dp[j])
		
		if ep[re[mxind]].count(x) == 0:
			ep[re[mxind]].insert(x)
		
		re[i] = re[mxind]
		dp[i] = ep[re[i]].mex()
		tansaku[i] = 2
	
ans = []

mada = [0]
tansaku = [0] * n
tansaku[0] = 1
gogo = [0] * n
while mada:
	i = mada.pop()
	if i >= 0:
		x = gogo[i]
		for j in ikeru[i]:
			if tansaku[j] == 1: continue
			x ^= dp[j]
		
		if x == 0:
			ans.append(i)
		
		for j in ikeru[i]:
			if tansaku[j] == 1: continue
			tansaku[j] = 1
			gogo[j] = x^dp[j]
			mada.append(j)		

if len(ans) == 0:
	print("Bob")
else:
	print("Alice")
	print(len(ans))
	ans.sort()
	print(*[x+1 for x in ans])
0