#P12958. [GCJ Farewell Round #3] Evolutionary Algorithms
[GCJ Farewell Round #3] Evolutionary Algorithms
题目描述
Ada is working on a science project for school. She is studying evolution and she would like to compare how different species of organisms would perform when trying to solve a coding competition problem.
The species are numbered with integers between 1 and , inclusive. Species 1 has no direct ancestor, and all other species have exactly one direct ancestor each, from which they directly evolved. A (not necessarily direct) ancestor of species is any other species such that can be reached from by moving one or more times to a species direct ancestor starting from . In this way, species 1 is a (direct or indirect) ancestor of every other species.
Through complex genetic simulations, she calculated the average score each of the species would get in a particular coding competition. is that average score for species .
Ada is looking for interesting triplets to showcase in her presentation. An interesting triplet is defined as an ordered triplet of distinct species such that:
- Species is a (direct or indirect) ancestor of species .
- Species is not a (direct or indirect) ancestor of species .
- Species has an average score strictly more than times higher than both of those of and . That is, $\mathbf{S}_b \geq \mathbf{K} \times \max(\mathbf{S}_a, \mathbf{S}_c) + 1$.
Given the species scores and ancestry relationships, help Ada by writing a program to count the total number of interesting triplets.
输入格式
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains two integers and , denoting the number of species and the factor which determines interesting triplets, respectively.
The second line of each test case contains integers $\mathbf{S}_1, \mathbf{S}_2, \ldots, \mathbf{S}_\mathbf{N}$, where denotes the average score of species .
The third line of each test case contains integers $\mathbf{P}_2, \mathbf{P}_3, \ldots, \mathbf{P}_\mathbf{N}$, meaning species is the direct ancestor of species .
输出格式
For each test case, output one line containing case #x: y
, where is the test case number (starting from 1) and is the total number of interesting triplets according to Ada's definition.
2
5 2
3 3 6 2 2
3 1 1 3
7 3
2 4 7 2 2 1 8
6 1 7 3 1 3
Case #1: 1
Case #2: 7
提示
Sample Explanation
In Sample Case #1, there is only one possible interesting triplet: . Indeed, we can verify that:
- Species is an ancestor of species .
- Species is not an ancestor of species .
- The score of species is more than times higher than the scores of both and : $6 = \mathbf{S}_3 \geq \mathbf{K} \times \max(\mathbf{S}_4, \mathbf{S}_5) + 1 = 2 \times \max(2, 2) + 1 = 5$.
In Sample Case #2, there are seven interesting triplets:
Limits
- .
- .
- , for all .
- , for all .
- Species 1 is a (direct or indirect) ancestor of all other species.
Test Set 1 (7 Pts, Visible Verdict)
- .
Test Set 2 (16 Pts, Hidden Verdict)
For at most 30 cases:
- .
For the remaining cases:
- .