#P4228. Possible Final Heart Positions in a Growing Tree

    ID: 17475 Type: Default 1000ms 256MiB

Possible Final Heart Positions in a Growing Tree

Possible Final Heart Positions in a Growing Tree

Consider a tree with n nodes (numbered from 1 to n), which is a rooted tree with node 1 as the root. The tree T is built gradually in n − 1 steps. Initially, the tree contains only node 1 and a special token called the "heart" is located at node 1.

At each subsequent step, a new node (which has not yet been added) is attached to the tree by selecting an edge connecting an already‐present node and a node not in the tree. When a new node v is added, the heart (which is currently at some node h) moves one step along the unique simple path from h to v (i.e. it moves from h to the neighbor of h on this path).

Because there are many valid orders in which the tree of n nodes can be grown, the final resting position of the heart may be different depending on the order chosen. Your task is: given the final (undirected) tree structure T, determine which nodes could possibly be the location of the heart when the growth process ends. (Note that by definition, node 1 will never be the final heart position.)

Note on the Unique Path: Since T is a tree, there is a unique simple path between any two vertices. In this problem, when the heart moves from its current location h to a newly added node v, it moves only one edge; that is, if the unique path from h to v is \( h = u_0, u_1, \dots, u_k = v \) then the heart is moved to u1.

Example. Consider a tree with 4 nodes and edges \( \{\langle 1,2\rangle,\langle 1,3\rangle,\langle 1,4\rangle\} \). One possible growth order to have the final heart at node 2 is:

  1. Step 1: Add node 3 by connecting it to node 1. The heart moves from 1 to 3.
  2. Step 2: Add node 4 by connecting it to node 1. The unique path from 3 to 4 is 3 \(\to\) 1 \(\to\) 4, so the heart moves from 3 to 1.
  3. Step 3: Add node 2 by connecting it to node 1. The heart moves from 1 to 2.

Similarly, other valid orders can lead to the final heart being at node 3 or node 4. It can be proved that node 1 is never attainable.

Your program should determine all possible nodes (in increasing order) that could be the final location of the heart given the tree structure.

inputFormat

The first line contains an integer n (2 ≤ n ≤ 10) — the number of nodes in the tree. (Note: For this problem, n is small.)

Each of the next n − 1 lines contains two integers u and v (1 ≤ u, vn, u ≠ v) indicating there is an undirected edge between nodes u and v.

outputFormat

Output all possible nodes (in increasing order) that could be the final resting position of the heart. The numbers should be separated by a space.

sample

4
1 2
1 3
1 4
2 3 4