#P16445. [XJTUPC 2026] The Whole Rest
[XJTUPC 2026] The Whole Rest
Problem Description
This is our story.
You are given a tree with vertices, numbered . Each edge has a non-negative integer weight .
A walk on the tree is defined as a vertex sequence () such that for every (), there is an edge . Note that vertices in the walk may repeat. That is, even if there exist with , it is still considered a valid walk.
The number of edges in the walk is defined as , i.e., the number of edges the walk traverses.
The set of vertices visited by the walk is defined as , i.e., all vertices that appear in the walk (duplicates are counted only once).
The cost of the walk is defined as the bitwise XOR of the weights of the edges along the walk: $w(v_1, v_2)\oplus w(v_2, v_3)\oplus w(v_3, v_4)\oplus\cdots\oplus w(v_{k-1},v_k)$, where denotes bitwise XOR. In particular, when (i.e., the walk contains only one vertex), the cost is .
You need to choose a walk that satisfies the following:
- It visits all vertices in the tree, i.e., the set of visited vertices equals all vertices.
- Among all walks satisfying condition 1, the cost of the walk is minimum.
- If multiple walks satisfy conditions 1 and 2, choose one with the minimum number of edges.
- If multiple walks satisfy conditions 1, 2, and 3, any of them will be accepted.
Output a walk that satisfies the above conditions, given as a vertex sequence.
Input Format
The first line contains an integer (), denoting the number of vertices in the given tree.
The next lines each contain three integers and (), indicating that there is an edge in the tree with weight . It is guaranteed that the given edges form a tree.
Output Format
Output two lines. The first line contains an integer (), denoting the length of the walk's vertex sequence.
The second line contains integers , separated by spaces, describing a walk that satisfies the conditions, whose vertex sequence is .
It can be proven that under the constraints of this problem:
- Every vertex label must appear at least once.
- For any walk satisfying conditions 1, 2, and 3, the length of its vertex sequence does not exceed .
4
1 2 0
1 3 2
3 4 2
4
4 3 1 2
6
1 2 1
3 6 5
3 1 2
2 5 3
4 2 4
8
3 6 3 1 2 4 2 5
Hint
Translated by ChatGPT 5