#P9536. [YsOI2023] Prüfer 序列

    ID: 10090 远端评测题 2000ms 150~512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP洛谷原创O2优化洛谷月赛

[YsOI2023] Prüfer 序列

Background

A counting problem for Ysuperman’s template testing.

As everyone knows, the Prüfer sequence has almost never appeared in official contests, so it needs to appear in a Luogu Monthly Contest.

Problem Description

As everyone knows, a labeled unrooted tree with nn nodes corresponds one-to-one with its Prüfer sequence. If you do not know what a Prüfer sequence is, you can refer to the explanation in the Hint section below.

Now you are given a positive integer sequence aa of length mm, where ai[1,n]a_i\in[1,n]. Uniformly at random, choose a subsequence of this sequence with length n2n-2 (as long as the chosen indices are different, the two subsequences are considered different) and use it as a Prüfer sequence to construct a tree TT. For all 1i<n1\le i<n, you need to compute the expected value of dist(i,n)\mathrm{dist}(i,n) (where dist(u,v)\mathrm{dist}(u,v) is defined as the number of edges on the simple path between nodes uu and vv).

Output the answer modulo 109+710^9+7.

Input Format

The first line contains two positive integers n,mn,m.

The second line contains mm integers representing the sequence aa, and it is guaranteed that 1ain1\le a_i\le n.

Output Format

Output n1n-1 non-negative integers. The ii-th integer denotes the expected value of dist(i,n)\mathrm{dist}(i,n).

4 3
2 4 3

2 666666673 1
6 4
4 6 5 2
4 1 1 3 2
10 12
6 9 2 10 1 5 5 9 10 7 8 3

585858594 60606064 8080810 834343444 638383846 785858595 913131322 595959602 286868692

Hint

Explanation of the Prüfer sequence

For a given unrooted labeled tree, the construction process of its Prüfer sequence is as follows: each time, select the leaf node with the smallest label and delete it, then record in the sequence the node it is connected to. Repeat this n2n-2 times; after that, only two nodes remain, and the recorded sequence is the Prüfer sequence of this unrooted labeled tree.

We can prove that an unrooted labeled tree with more than 11 node corresponds one-to-one with a Prüfer sequence, and that any Prüfer sequence of length n2n-2 with each number being an integer in [1,n][1,n] has a unique corresponding tree. Similarly, we can also restore a tree from a Prüfer sequence.

For more detailed content about the Prüfer sequence, you can refer to the description of the Prüfer sequence on OI-wiki.

Sample 1 explanation

There are three equally likely subsequences to choose from: 2,42,4, 2,32,3, 4,34,3.

  1. The tree corresponding to 2,42,4 is a chain 12431-2-4-3, and the distances from 1,2,31,2,3 to 44 are 2,1,12,1,1, respectively.
  2. The tree corresponding to 2,32,3 is a chain 12341-2-3-4, and the distances from 1,2,31,2,3 to 44 are 3,2,13,2,1, respectively.
  3. The tree corresponding to 4,34,3 is a chain 14321-4-3-2, and the distances from 1,2,31,2,3 to 44 are 1,2,11,2,1, respectively.

Therefore, the expected value of dist(1,4)\mathrm{dist}(1,4) is (2+3+1)/3=2(2+3+1)/3=2, the expected value of dist(2,4)\mathrm{dist}(2,4) is (1+2+2)/3=5/3666666673(mod109+7)(1+2+2)/3=5/3\equiv 666666673\pmod{10^9+7}, and the expected value of dist(3,4)\mathrm{dist}(3,4) is (1+1+1)/3=1(1+1+1)/3=1.

Sample 2 explanation

There is only one possible subsequence 4,6,5,24,6,5,2, and the corresponding tree is a chain 1452631-4-5-2-6-3. The distances from 1,2,3,4,51,2,3,4,5 to 66 are 4,1,1,3,24,1,1,3,2 in order, which is the answer.

Constraints

This problem has a total of 2020 test points:

Test Point ID nn mm
11 =4=4 =6=6
22 =8=8 =15=15
33 =10=10 =20=20
44 =50=50
55 =200=200
66 =1000=1000
77 =1750=1750
88 =2500=2500
99 =11=11 =500=500
1010 =1000=1000
1111 =12=12 =250=250
1212 =375=375
1313 =400=400
1414 =13=13 =80=80
1515 =120=120
1616 =160=160
1717 =200=200
1818 =14=14 =60=60
1919 =90=90
2020 =15=15 =40=40

In addition, for all testdata, it is guaranteed that 1ain1\le a_i\le n.

Please note that the memory limit for the first 1919 test points is 512MB512\rm{MB}, and the memory limit for the last test point is 150MB150\rm{MB}.

Translated by ChatGPT 5