logo AlgoBeat OnlineJudge
登录 注册

#213063. [GCJ Farewell Round #3] Evolutionary Algorithms

内存限制:2048 MiB 时间限制:40000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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:

  1. Species is a (direct or indirect) ancestor of species .
  2. Species is not a (direct or indirect) ancestor of species .
  3. Species has an average score strictly more than times higher than both of those of and . That is, .

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 , where denotes the average score of species .

The third line of each test case contains integers , 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.

样例

样例输入 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

样例输出 1

Case #1: 1
Case #2: 7

数据范围与提示

Sample Explanation

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

  1. Species is an ancestor of species .
  2. Species is not an ancestor of species .
  3. The score of species is more than times higher than the scores of both and : .

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:

  • .