#P10212. [CTS2024] 众生之门

[CTS2024] 众生之门

Background

Original statement archive.

Problem Description

Given nn, an undirected tree with nn nodes, and two different nodes s,ts,t on the tree. Each edge has length 11. Nodes are labeled with integers from 11 to nn. Let dist(u,v)\mathrm{dist}(u,v) denote the distance between uu and vv (i.e., the number of edges on the simple path). You need to output a permutation p1,p2,,pnp_1,p_2,\cdots,p_n of 11 to nn that satisfies the following two conditions:

  • p1=sp_1=s, pn=tp_n=t.
  • Let di=dist(pi,pi+1)(1in1)d_i=\mathrm{dist}(p_i,p_{i+1})(1\leq i\leq n-1). Under the above conditions, your permutation should minimize i=1n1di\oplus_{i = 1} ^ {n - 1}d_i, where \oplus denotes bitwise XOR.

If there are multiple valid permutations, you may output any one of them.

Input Format

Read input from standard input.

This problem contains multiple test cases. The first line contains a positive integer TT, the number of test cases.

For each test case, the first line contains three positive integers n,s,tn,s,t. The next n1n-1 lines each contain two positive integers u,vu,v, indicating that there is a direct undirected road between uu and vv (i.e., an edge of the tree).

Output Format

Write output to standard output.

For each test case, output one line of nn positive integers p1,p2,,pnp_1,p_2,\cdots,p_n. You must ensure it is a permutation of 11 to nn with p1=sp_1=s and pn=tp_n=t, and that i=1n1di\oplus_{i = 1} ^ {n - 1} d_i is minimized.

3
3 1 3
1 2
2 3
4 3 4
1 2
2 3
2 4
5 1 2
1 2
1 3
2 4
3 5

1 2 3
3 2 1 4
1 5 3 4 2
见题目目录下的 2.in 与 2.ans。

Hint

Sample 1 Explanation

This sample contains three test cases.

  • For the first test case, the minimum possible value of i=1n1di\oplus_{i = 1} ^ {n - 1} d_i is 00. The sample output is 1,2,31,2,3, where d1=d2=1d_1=d_2=1.
  • For the second test case, the minimum possible value of i=1n1di\oplus_{i = 1} ^ {n - 1} d_i is 22. The sample output is 3,2,1,43,2,1,4, where d1=d2=1d_1=d_2=1 and d3=2d_3=2; another valid output is 3,1,2,43,1,2,4.
  • For the third test case, the minimum possible value of i=1n1di\oplus_{i = 1} ^ {n - 1} d_i is 11. The sample output is 1,5,3,4,21,5,3,4,2, where d1=2d_1=2, d2=1d_2=1, d3=3d_3=3, d4=1d_4=1.

Sample 2 Explanation

Note that this input consists of all tree shapes that satisfy n10n\leq 10 and all possible relative positions of different ss and tt.

This sample is undoubtedly a selfless gift from the kind problem setter. I forgot what happened in the middle. The problem setter believes that this wonderful sample can provide strong help to you on your dream-chasing road of getting AC on this problem.

Constraints

Let n\sum n denote the sum of nn over all test cases in a single test file. For all testdata,

  • T1T\ge 1.
  • 2n5×1042\leq n\leq 5\times 10 ^4, n5×105\sum n\leq 5\times 10 ^ 5.
  • 1s,tn1\leq s,t\leq n, sts\neq t.
  • 1u,vn1\le u,v\le n, uvu\neq v.
  • The n1n-1 input pairs (u,v)(u,v) form a tree.
Subtask ID nn n\sum n Special Property Score
1 8\le 8 103\le 10^{3} None 5
2 12\le 12 8
3 5×104\le 5 \times 10^{4} 5×105\le 5 \times 10^{5} A 17
4 B 20
5 C 17
6 103\le 10^{3} 104\le 10^{4} D 10
7 5×104\le 5 \times 10^{4} 5×105\le 5 \times 10^{5} None 23

Special Property A: It is guaranteed that the degree of each node is at most 22.

Special Property B: It is guaranteed that for any xx, $\mathrm{dist}(s,x)+\mathrm{dist}(x,t)\le \mathrm{dist}(s,t)+2$.

Special Property C: It is guaranteed that dist(s,t)=1\mathrm{dist}(s,t)=1.

Special Property D: This subtask has five test points. For each test point, it is guaranteed that T=10T=10 and n=1000n=1000. In each test case, ss is generated uniformly at random from 1n1\sim n, tt is generated uniformly at random from 1n1\sim n excluding ss, and the tree is generated uniformly at random from all labeled trees on nn nodes.

Translated by ChatGPT 5