CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:ziihrs
Submission time:2021-01-31 18:30:59 +0200
Language:Python3 (PyPy3)
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
Test results
testverdicttimegroup
#1ACCEPTED0.18 s1, 2details
#2--2details

Code

from random import shuffle
import sys
input = sys.stdin.readline

for _ in range(int(input())):
	N = int(input())
	adj = [[] for _ in range(N)]
	for _ in range(N-1):
		a, b = map(lambda s: int(s)-1, input().split())
		adj[a].append(b)
		adj[b].append(a)


	D = [[-1] * N for _ in range(N)]
	A = [[] for _ in range(N)]
	for s in range(N):
		Q = [s]
		MD = D[s]
		MD[s] = 0
		for i in Q:
			for j in adj[i]:
				if MD[j] == -1:
					MD[j] = MD[i] + 1
					Q.append(j)

		for i in range(N):
			if 1 <= MD[i] <= 3:
				A[s].append(i)

		shuffle(A[s])


	V = [False] * N
	V[0] = True
	order = [0]

	def f(i):
		if len(order) == N:
			return True

		for j in A[i]:
			if not V[j]:
				V[j] = True
				order.append(j)
				if f(j): return True
				V[j] = False
				order.pop()

		return False

	f(0)

	print(' '.join(str(v+1) for v in order))

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
100
8
5 2
2 3
3 7
...

correct output
1 8 2 5 6 7 3 4 
1 7 2 8 3 6 4 5 
1 4 6 2 7 5 8 3 
1 8 3 2 4 7 6 5 
1 6 4 7 5 2 3 8 
...

user output
1 8 7 4 2 6 3 5
1 4 3 2 8 7 5 6
1 6 5 2 7 8 3 4
1 5 6 3 4 2 7 8
1 8 6 4 3 5 7 2
...
Truncated

Test 2

Group: 2

Verdict:

input
100
100
37 59
81 37
44 81
...

correct output
1 99 82 81 59 5 71 55 17 24 13...

user output
(empty)