Eikooc and Sushi play a game.

The game is played on a tree having $n$ nodes numbered $1$ to $n$. Recall that a tree having $n$ nodes is an undirected, connected graph with $n-1$ edges.

They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $u$ such that

$u$ is adjacent to the node $v$ the token is currently on

$u$ has not been visited before

$u \oplus v \leq min(u, v)$

Here $x \oplus y$ denotes the bitwise XOR operation on integers $x$ and $y$.

Both the players play optimally. The player who is unable to make a move loses.

The following are examples which demonstrate the rules of the game.

Suppose Eikooc starts the game by placing the token at node $4$. Sushi then moves the token to node $6$, which is unvisited and adjacent to $4$. It also holds that $6 \oplus 4 = 2 \leq min(6, 4)$. In the next turn, Eikooc moves the token to node $5$, which is unvisited and adjacent to $6$. It holds that $5 \oplus 6 = 3 \leq min(5, 6)$. Sushi has no more moves to play, so she loses.

Suppose Eikooc starts the game by placing the token at node $3$. Sushi moves the token to node $2$, which is unvisited and adjacent to $3$. It also holds that $3 \oplus 2 = 1 \leq min(3, 2)$. Eikooc cannot move the token to node $6$ since $6 \oplus 2 = 4 \nleq min(6, 2)$. Since Eikooc has no moves to play, she loses.

Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $p$ of length $n$ (sequence of $n$ integers wherein each integer from $1$ to $n$ occurs exactly once) where $p_i$ denotes the new numbering of node $i$.

She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so.


-----Input-----

The first line contains a single integer $t~(1 \le t \le 10^5)$  — the number of test cases. The description of each test case is as follows.

The first line of each test case contains an integer $n~(1 \le n \le 2 \cdot 10^5)$  — the number of nodes in the tree.

The next $n-1$ lines contain two integers $u$ and $v$ $(1 \le u, v \le n; u \neq v)$  — denoting an edge between nodes $u$ and $v$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case print any suitable relabeling  — a permutation of length $n$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them.


-----Examples-----

Input
3
1
2
1 2
3
1 2
1 3
Output
1
2 1
1 2 3


-----Note-----

In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win.

In the second test case, $1 \oplus 2 = 3 \nleq min(1, 2)$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $\{1, 2\}$ and $\{2, 1\}$ are optimal relabelings.

Source: codeforces | Difficulty: HARD
Tags: Bit manipulation

Graph traversal

Constructive algorithms

Implementation

Greedy algorithms

Tree algorithms

Game theory


Solutions:
import sys
from collections import deque

def solve(n, edges):
	vis = [-1] * (n + 1)
	g = [set() for _ in range(n + 1)]
	for (u, v) in edges:
		g[u].add(v)
		g[v].add(u)
	subs = [[], []]
	for i in range(1, n + 1):
		if vis[i] < 0:
			q = deque([i])
			vis[i] = 0
			subs[0].append(i)
			while q:
				u = q.popleft()
				for v in g[u]:
					if vis[v] < 0:
						g[v].remove(u)
						vis[v] = 1 - vis[u]
						q.append(v)
						subs[vis[v]].append(v)
	subs.sort(key=lambda it: len(it))
	m = len(subs[0])
	val = []
	while m:
		b = m & -m
		val.extend(list(range(b, b << 1)))
		m -= b
	st = set(val)
	for i in subs[0]:
		vis[i] = val.pop()
	k = 1
	for i in subs[1]:
		while k in st:
			k += 1
		vis[i] = k
		k += 1
	return ' '.join(map(str, vis[1:]))
t = int(sys.stdin.readline())
ans = []
for _ in range(t):
	n = int(sys.stdin.readline())
	edges = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n - 1)]
	ans.append(solve(n, edges))
print('\n'.join(ans))


from collections import deque
for _ in range(int(input())):
	N = int(input())
	I = [[] for _ in range(N)]
	for _ in range(N - 1):
		(u, v) = map(int, input().split())
		u -= 1
		v -= 1
		I[u].append(v)
		I[v].append(u)
	state = [-1] * N
	s = 0
	state[s] = 0
	task = deque([s])
	while task:
		p = task.pop()
		for q in I[p]:
			if state[q] != -1:
				continue
			state[q] = 1 ^ state[p]
			task.append(q)
	g0 = []
	g1 = []
	for (i, s) in enumerate(state):
		if s:
			g1.append(i)
		else:
			g0.append(i)
	if len(g1) > len(g0):
		(g1, g0) = (g0, g1)
	p1 = []
	t1 = len(g1)
	ans = [-1] * N
	idx0 = 0
	idx1 = 0
	now = 1
	while t1 > 0:
		if t1 & 1:
			for j in range(now, 2 * now):
				ans[g1[idx1]] = j
				idx1 += 1
		else:
			for j in range(now, 2 * now):
				ans[g0[idx0]] = j
				idx0 += 1
		t1 >>= 1
		now *= 2
	for j in range(now, N + 1):
		ans[g0[idx0]] = j
		idx0 += 1
	print(*ans)


def solution():
	n = int(input())
	adj = [[] for _ in range(n)]
	for _ in range(n - 1):
		(u, v) = map(lambda x: int(x) - 1, input().strip().split())
		adj[u].append(v)
		adj[v].append(u)
	colors = [-1] * n
	stk = [0]
	colors[0] = 0
	while stk:
		u = stk.pop()
		for v in adj[u]:
			if colors[v] != -1:
				continue
			colors[v] = colors[u] ^ 1
			stk.append(v)
	(w, color) = (sum(colors), 1)
	if w > n - w:
		(w, color) = (n - w, 0)
	result = [0] * n
	idx1 = [i for (i, c) in enumerate(colors) if c == color]
	idx2 = [j for (j, c) in enumerate(colors) if c != color]
	bit = 1
	i = j = 0
	while bit <= n:
		if w & bit:
			for x in range(bit, min(2 * bit - 1, n) + 1):
				result[idx1[i]] = x
				i += 1
		else:
			for x in range(bit, min(2 * bit - 1, n) + 1):
				result[idx2[j]] = x
				j += 1
		bit <<= 1
	return ' '.join(map(lambda x: str(x), result))
for case in range(int(input())):
	print('%s' % solution())


(p, r) = (input, range)
for _ in r(int(p())):
	n = int(p())
	h = [set() for i in r(n)]
	if n == 1:
		print(1)
		continue
	for i in r(n - 1):
		(u, v) = map(int, p().split())
		h[u - 1].add(v - 1)
		h[v - 1].add(u - 1)
	(c, s, g, i) = ([len(h[i]) for i in r(n)], [0] * n, [0], 1)
	while 2 ** i - 1 < n:
		g.append(2 ** i - 1)
		i += 1
	g.append(n)
	l = [i for i in r(n) if c[i] == 1]
	while l != []:
		b = []
		for v in l:
			(c[v], e) = (0, set())
			for u in h[v]:
				if c[u] > 0:
					c[u] -= 1
					if c[u] == 1:
						b.append(u)
				elif s[u] != 0:
					e.add(len(bin(s[u])) - 2)
			for i in r(len(g) - 1, 0, -1):
				if i not in e and g[i] >= 2 ** (i - 1):
					s[v] = g[i]
					g[i] -= 1
					break
		l = b
	print(' '.join([str(c) for c in s]))