luogu#P5090. [eJOI 2018] 互素树

    ID: 4111 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018提交答案Special JudgeeJOI(欧洲)

[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 nn nodes, numbered from 11 to nn.
For any edge (u,v)(u, v), if there exists a positive integer d>1d > 1 such that dud \mid u and dvd \mid v, then we call it a bad edge.
In the tree shown below, there are three bad edges: (6,4)(6, 4) (both divisible by 22), (2,6)(2, 6) (both divisible by 22), and (3,6)(3, 6) (both divisible by 33).

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, (3,6)(3, 6).

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 TT, the number of testdata groups.
Each group of testdata has nn lines in total, where nn is the number of nodes in the tree.
The first line contains an integer nn.
The next n1n - 1 lines each contain two integers uu and v (1u,vn)v \ (1 \le u, v \le n), representing an edge (u,v)(u, v).

In each input file, all trees have the same number of nodes.

Output Format

For each group of testdata, output one line with nn integers, representing the new labels of the nodes originally labeled from 11 to nn.
All node labels must be different. That is, the nn 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 M=T×(n1)M = T \times (n - 1). Let the number of bad edges in your output be XX, and define R=XMR = \dfrac{X}{M}. Your score as a function of RR is as follows:

RR Score RR Score
0.33<R0.40.33 < R \le 0.4 11 0.01<R0.050.01 < R \le 0.05 66
0.26<R0.330.26 < R \le 0.33 22 0.005<R0.010.005 < R \le 0.01 77
0.19<R0.260.19 < R \le 0.26 33 0.001<R0.0050.001 < R \le 0.005 88
0.12<R0.190.12 < R \le 0.19 44 0<R0.0010 < R \le 0.001 99
0.05<R0.120.05 < R \le 0.12 55 R=0R = 0 1010

When R>0.4R > 0.4, you get no score.

For all test points, it is guaranteed that an output with X=0X = 0 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 (3,6)(3, 6), 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, M=2×5=10M = 2 \times 5 = 10.

For Output 1, X=1,R=110=0.1X = 1, R = \dfrac{1}{10} = 0.1, so this output gets 55 points.

For Output 2, X=0,R=010=0X = 0, R = \dfrac{0}{10} = 0, so this output gets 1010 points.


Constraints and Limits

Limits

  • For test point 1 (input 01), T=3,n=7T = 3, n = 7. The three trees are as follows:

  • For test points 4 to 8 (inputs 04 to 08), 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
TT 33 100100 500500 200200 5050 1010 55 22 11
nn 77 1010 3030 100100 500500 20002000 1000010000 2000020000 5000050000 100000100000

Translated by ChatGPT 5