luogu#P5090. [eJOI 2018] 互素树
[eJOI 2018] 互素树
Background
This problem is translated from eJOI2018 Problem E "Prime Tree".
Problem Description
This is an output-only problem. The input files are provided in the attachments.
There is a tree with nodes, numbered from to .
For any edge , if there exists a positive integer such that and , then we call it a bad edge.
In the tree shown below, there are three bad edges: (both divisible by ), (both divisible by ), and (both divisible by ).

Your task is to renumber the nodes so that the number of bad edges is as small as possible.
For the tree above, if you renumber the nodes as in the figure below, there will be only one bad edge left, .

After renumbering, the fewer bad edges you have, the higher your score will be.
This is an output-only problem. You should download the output file, then run your program locally and upload the output result. Of course, on Luogu you may also submit your program directly.
Input Format
Each test point contains multiple groups of testdata.
The first line contains an integer , the number of testdata groups.
Each group of testdata has lines in total, where is the number of nodes in the tree.
The first line contains an integer .
The next lines each contain two integers and , representing an edge .
In each input file, all trees have the same number of nodes.
Output Format
For each group of testdata, output one line with integers, representing the new labels of the nodes originally labeled from to .
All node labels must be different. That is, the integers you output for the same group of testdata must be pairwise distinct.
2
6
1 3
3 5
3 6
6 4
6 2
6
1 2
1 3
1 4
1 5
1 6
2 5 3 1 4 6
5 1 2 3 4 6
4 5 1 3 6 2
5 4 6 1 3 2
Hint
Scoring
For each test point, let the total number of edges in all trees be . Let the number of bad edges in your output be , and define . Your score as a function of is as follows:
| Score | Score | ||
|---|---|---|---|
When , you get no score.
For all test points, it is guaranteed that an output with exists.
Sample Explanation
Note that the sample provides two valid outputs. For convenience, call them Output 1 and Output 2.
The first group of testdata has already been discussed in the statement. In Output 1 there is one bad edge , while in Output 2 there are no bad edges.
In the second group of testdata, neither Output 1 nor Output 2 contains any bad edges.
In the sample, .
For Output 1, , so this output gets points.
For Output 2, , so this output gets points.
Constraints and Limits
Limits
- For test point 1 (input
01), . The three trees are as follows:

-
For test points 4 to 8 (inputs
04to08), the input has special properties (for example, many leaf nodes, being a binary tree, etc.), and trees with these special properties are evenly distributed within each test point. -
For the other test points, the data is randomly generated.
Constraints
| Test Point | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Filename | 01 |
02 |
03 |
04 |
05 |
06 |
07 |
08 |
09 |
10 |
Translated by ChatGPT 5