luogu#P5036. 随机生成树

随机生成树

Background

An easy problem adapted by @Gejun.

Problem Description

Rainheavy drew NN points on paper (numbered from 11 to NN). The color of each point is described by an integer. Rainheavy decides to randomly generate a tree using these NN points. The rules are as follows.

For points 22 to NN, each point randomly chooses one point to connect to. The point that node ii (2iN)(2 \le i \le N) connects to is randomly selected from: the divisors of ii, and the multiples of ii that do not exceed NN. (For example, when N=30N=30, node 1010 can connect to node 11, 22, 55, 2020, or 3030.)

The generated tree cannot have multiple edges. (Otherwise it would not be a tree.)

After the tree is generated, Rainheavy can compute how many connected components it has. A connected component is a set of points that must satisfy the following two conditions.

  1. For any two points chosen from the set: the two points have the same color, and there exists a path consisting of tree edges between them such that all points on the path have the same color as these two points.

  2. For any point inside the set and any point outside the set: the two points are either of different colors, or there does not exist a path consisting of tree edges between them such that all points on the path have the same color as these two points.

Rainheavy wants to find, when the number of connected components in the generated tree is maximized, which edges should be connected. However, Rainheavy is too strong and does not bother with such a boring problem. More importantly, he is going to AK IOI, so he throws the problem to you.

Note: the order of edges.

  1. First, prioritize maximizing the number of connected components (i.e., prioritize edges that contribute to forming connected components).

  2. If condition 1 is tied, prioritize the edge with the smaller sum of the two endpoint indices. (For example, under condition 1, the edge connecting node 33 and node 55 has higher priority than the edge connecting node 44 and node 55.)

  3. If condition 2 is also tied, prioritize the edge whose smaller endpoint index is smaller. (For example, under condition 2, the edge connecting node 22 and node 66 has higher priority than the edge connecting node 33 and node 55.)

Input Format

The first line contains an integer NN, representing the number of nodes.

The second line contains NN integers. The ii-th integer cic_i represents the color of node ii.

Output Format

Output a total of N1N-1 lines. Each line contains two integers x,yx,y, indicating that an edge should connect node xx and node yy. (It is required that x<yx<y, and the output order must satisfy the edge order described in the statement.)

Tip: Since the output is large, fast output is recommended. (If you cannot, then forget it, at worst you will get TLE, right.)

4
3 2 3 2
1 2
1 4
1 3

Hint

Explanation for the sample: Because nodes 22 and 44 will contribute to forming connected components (connecting node 33 is useless), and since 1+2<1+41+2 \lt 1+4, (1,2)(1,2) should be output before (1,4)(1,4), and finally output (1,3)(1,3).

Constraints:

For 30%30\% of the testdata, 2N102 \le N \le 10.
For 60%60\% of the testdata, 2N50002 \le N \le 5000.
For 80%80\% of the testdata, 2N2×1052 \le N \le 2 \times 10^5.
For 100%100\% of the testdata, 2N5×1052 \le N \le 5 \times 10^5, 1ci1091 \le c_i \le 10^9. (Anyway, bigger is useless.)

Translated by ChatGPT 5