#P8260. [CTS2022] 燃烧的呐球
[CTS2022] 燃烧的呐球
Background
Warning: Abusing this problem to hack the judge will result in an account ban.
Problem Description
You are given a rooted tree with vertices, and ordered pairs , where are vertices of the tree.
For vertices in the tree, define as the number of vertices that are in the subtree rooted at , but not in the subtree rooted at .
You need to find the minimum spanning tree of the complete graph whose vertices are these ordered pairs. The edge weight between and is .
Input Format
The first line contains two integers .
The next line contains integers. The -th integer denotes the parent of node , and it is guaranteed that .
Then follow lines. The -th line contains two integers , representing a given ordered pair.
Output Format
Output one integer, the sum of edge weights of the minimum spanning tree.
5 4
1 2 3 3
3 5
2 2
5 2
2 5
7
Hint
Explanation of the sample:
The minimum spanning tree contains edges . Each triple means: the index of the first ordered pair, the index of the second ordered pair, and the edge weight. The total weight is .
Constraints:
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, , and all ordered pairs are distinct.
For another of the testdata, for all , .
For another of the testdata, for all , .
For of the testdata, . For all , . For all , .
Translated by ChatGPT 5