Evolutionary Algorithms

시간 제한40초메모리 제한1024 MB

요약
b가 a의 조상이지만 c의 조상이 아니고, S_b가 S_a와 S_c 각각의 K배보다 큰 순서 있는 삼중항 (a,b,c)의 개수를 센다.
난이도

어려움10점 중 8점

유형
트리, DFS, 정렬, 분할 정복
정답자
아직 제출이 없습니다

문제

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 N\mathbf{N} species are numbered with integers between 11 and N\mathbf{N}, inclusive. Species 11 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 xx is any other species yy such that yy can be reached from xx by moving one or more times to a species direct ancestor starting from xx. In this way, species 11 is a (direct or indirect) ancestor of every other species.

Through complex genetic simulations, she calculated the average score each of the N\mathbf{N} species would get in a particular coding competition. S_i\mathbf{S\_i} is that average score for species ii.

Ada is looking for interesting triplets to showcase in her presentation. An interesting triplet is defined as an ordered triplet of distinct species (a,b,c)(a, b, c) such that:

  1. Species bb is a (direct or indirect) ancestor of species aa.
  2. Species bb is not a (direct or indirect) ancestor of species cc.
  3. Species bb has an average score strictly more than K\mathbf{K} times higher than both of those of aa and cc. That is, S_b≥K×max⁡(S_a,S_c)+1\mathbf{S\_b} \ge \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, T\mathbf{T}. T\mathbf{T} test cases follow.

The first line of each test case contains two integers N\mathbf{N} and K\mathbf{K}, denoting the number of species and the factor which determines interesting triplets, respectively.

The second line of each test case contains N\mathbf{N} integers S_1,S_2,…,S_N\mathbf{S\_1}, \mathbf{S\_2}, \dots, \mathbf{S\_N}, where S_i\mathbf{S\_i} denotes the average score of species ii.

The third line of each test case contains N−1\mathbf{N}-1 integers P_2,P_3,…,P_N\mathbf{P\_2}, \mathbf{P\_3}, \dots, \mathbf{P\_N}, meaning species P_i\mathbf{P\_i} is the direct ancestor of species ii.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the total number of interesting triplets according to Ada's definition.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤K≤1091 \le \mathbf{K} \le 10^9.
  • 1≤S_i≤1091 \le \mathbf{S\_i} \le 10^9, for all ii.
  • 1≤P_i≤N1 \le \mathbf{P\_i} \le \mathbf{N}, for all ii.
  • Species 11 is a (direct or indirect) ancestor of all other species.

힌트

In Sample Case #1, there is only one possible interesting triplet: (5,3,4)(5, 3, 4). Indeed, we can verify that:

  1. Species b=3b = 3 is an ancestor of species a=5a = 5.
  2. Species b=3b = 3 is not an ancestor of species c=4c = 4.
  3. The score of species b=3b = 3 is more than K\mathbf{K} times higher than the scores of both a=5a = 5 and c=4c = 4: 6=S_3≥K×max⁡(S_4,S_5)+1=2×max⁡(2,2)+1=56 = \mathbf{S\_3} \ge \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:

  • (4,3,1)(4, 3, 1)
  • (4,3,6)(4, 3, 6)
  • (4,7,1)(4, 7, 1)
  • (4,7,5)(4, 7, 5)
  • (4,7,6)(4, 7, 6)
  • (5,3,1)(5, 3, 1)
  • (5,3,6)(5, 3, 6)

예제1

  1. 예제 1

    입력
    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