Under construction

Still being built. Things may change

Prime Tree - 7

Time limit10sMemory limit512 MB

Summary
Relabel the vertices of each given tree with 1..n so that the number of edges whose endpoints share a common divisor above 1 is minimized, and submit the answer file.
Level

Hard9 of 10

Topics
Greedy, Number theory, Tree, DFS
Solved
No attempts yet

Problem

A tree is a connected undirected graph that has no cycles. Consider a tree with n vertices, labeled with the integers 1, 2, ..., n. Call an edge (u, v) bad if there is an integer d > 1 such that the label of u and the label of v are both divisible by d. For example, the tree below has three bad edges: (6, 4) are both divisible by 2, (2, 6) are both divisible by 2, and (3, 6) are both divisible by 3.

Your goal is to relabel the vertices so that the number of bad edges is as small as possible. For example, if you relabel the vertices of the tree above as follows, only one bad edge remains, (3, 6).

The fewer bad edges your tree has, the more points you get.

This is an output-only problem. Run your program locally and submit only the answer file for each input file.

Input

Each input file contains several test cases.

The first line of the input file contains the number of test cases in that file.

The first line of a test case contains a single integer n, the number of vertices in the tree.

Each of the following n − 1 lines contains two integers u and v (1 ≤ u, v ≤ n), the vertices connected by an edge.

All trees in a single file have the same number of vertices.

Output

For each test case, print one line containing exactly n different integers from 1 to n: the labels assigned to vertices 1, 2, . . . , n.

Hint

The first test case is shown in the problem statement above. After relabeling there is one bad edge, (6, 3), because 6 and 3 are both divisible by 3.

The second test case has the edges (5, 1), (5, 2), (5, 3), (5, 4), and (5, 6). None of them are bad.

The input file has 10 edges and the answer has 1 bad edge. Thus M = 10, X = 1, R = 0.1. According to the scoring table, this answer would get 5 points.

The tests have the following structure:

  • Input file 1 contains three trees on 7 vertices, drawn below from left to right.

  • Input files 2 and 3 contain 100 random trees on 10 and 30 vertices respectively.
  • Input files 4 through 8 contain various randomly generated trees with special structure (for example, trees with many leaves, binary trees). The distribution of tree types is roughly the same for every input.
  • Input files 9 and 10 contain randomly generated trees of 50,000 and 100,000 vertices respectively.

Initially, the vertex labels of all trees in all input files are random.

Grading uses data-7.in from the archive.

Examples1

  1. Example 1

    Input
    2
    6
    1 3
    3 5
    3 6
    6 4
    6 2
    6
    1 2
    1 3
    1 4
    1 5
    1 6
    
    Expected output
    2 5 3 1 4 6
    5 1 2 3 4 6