Under construction

Still being built. Things may change

Evolutionary Algorithms

Time limit40sMemory limit1024 MB

Summary
Count ordered triplets (a, b, c) where b is an ancestor of a but not of c, and S_b is at least K times both S_a and S_c plus 1.
Level

Medium7 of 10

Topics
Tree, DFS, Binary search, Sorting
Solved
No attempts yet

Problem

Ada is working on a science project for school. She is studying evolution and wants to compare how different species perform when solving a coding competition problem.

There are N\mathbf{N} species numbered from 1 to N\mathbf{N}. Species 1 has no direct ancestor, and every other species has exactly one direct ancestor, from which it evolved. An ancestor of species xx (direct or indirect) is any other species yy that can be reached from xx by repeatedly moving to a direct ancestor. So species 1 is an ancestor of every other species.

Each species ii has an average score S_i\mathbf{S\_i} in a particular coding competition. Ada calculated these scores with genetic simulations.

An interesting triplet is an ordered triplet of distinct species (a,b,c)(a, b, c) such that:

  1. Species bb is an ancestor of species aa.
  2. Species bb is not an ancestor of species cc.
  3. Species bb has an average score strictly more than K\mathbf{K} times higher than both 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.

Count the interesting triplets.

Input

The first line contains T\mathbf{T}, the number of test cases. Each test case is given as follows.

The first line contains two integers N\mathbf{N} and K\mathbf{K}.

The second line contains N\mathbf{N} integers S_1,S_2,…,S_N\mathbf{S\_1}, \mathbf{S\_2}, \dots, \mathbf{S\_N}.

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

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1, and yy is the total number of interesting triplets.

Constraints

  • 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 1 is a (direct or indirect) ancestor of all other species.

Hint

In Sample Case #1, the only interesting triplet is (5,3,4)(5, 3, 4). Species 3 is an ancestor of species 5, and it is not an ancestor of species 4. Also, 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)

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    Case #1: 1
    Case #2: 7