Problem Description


In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?


Input


The first line of the input contains an integer T( T<= 5 ), indicating the number of test cases.
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 10 5, 1 <= K <= N )
Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 10 9 )
For next N-1 lines, each line contains two vertices u and v, which is connected in the tree.
Next line is a integer Q, representing the number of queries. (1 <= Q <= 10 5)
For next Q lines, each with an integer u, as the root of the subtree described above.


Output


For each test case, output "Case #X:" first, X is the test number. Then output Q lines, each with a number -- the answer to each query.

Seperate each test case with an empty line.


Sample Input


1 3 1 1 2 2 1 2 1 3 3 2 1 3


Sample Output


Case #1:
1
1
1


给定一棵树,每个点有一个标号和权值,标号为1是的是树的根,给出k,询问标号为x的点的子树中(包括x),

有几个不同的权值刚好有k个。

首先把树转化成序列,遍历一遍然后记录下每个标号的起始位置和结束位置,这样子树就转化为了这一段区间。

接下来考虑怎么求出区间里有多少个值恰好出现k次。

由于给出的点的权值在0到10亿,而点数只有10万个,所以先进行离散化。

这样来考虑,当前的点的值是x的话,那么会影响到的区间则是右端在当前x位置且在下一个x位置前的,左端在

往前k个x处的前后的。

于是用一个数组来表示p[i][j],代表值为i的出现了j次的位置。

那么如果当前的x出现了y次,那么左端在区间(p[x][y-k],p[x][y-k+1]],右端不包括下一个x的点的询问都会+1

同理左端在区间(p[x][y-k-1],p[x][y-k]],右端不包括下一个x的点的询问都会-1

那么就可以利用树状数组来统计了。

离线处理询问,按照点的右端排序。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
const int maxn = 100005;
const int low(int x){ return x&-x; }
map<int, int> M;
vector<int> tree[maxn], p[maxn];
int T, n, k, a[maxn], m, tot, b[maxn], f[maxn], ans[maxn];
struct abc
{
int a, b, c;
bool operator <(const abc&x)
{
if (x.c == c) return b < x.b;
return c < x.c;
}
}w[maxn], r[maxn];

void work(int x, int fa)
{
w[x].b = ++tot; b[tot] = a[x];
for (int i = 0; i < tree[x].size();i++)
if (tree[x][i] != fa) work(tree[x][i], x);
w[x].c = tot;
}

void add(int x, int y)
{
for (int i = x; i <= n; i += low(i)) f[i] += y;
}

int sum(int x)
{
int tot = 0;
for (; x; x -= low(x)) tot += f[x];
return tot;
}

int main()
{
scanf("%d", &T);
for (int t = 0; T - t; t++)
{
cin >> n >> k;
int x, y = 0;
M.clear();
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++)
{
scanf("%d", &x); tree[i].clear();
p[i].clear(); p[i].push_back(0);
if (M.count(x)) a[i] = M[x]; else a[i] = M[x] = ++y;
}
//离散化点的权值顺带初始化数组

for (int i = 1; i < n; i++)
{
scanf("%d%d", &x, &y);
tree[x].push_back(y);
tree[y].push_back(x);
}
//用vector建树

tot = 0; work(1, 1);

scanf("%d", &m);
for (int i = 1; i <= m; i++)
{
scanf("%d", &x);
r[i] = w[x];
r[i].a = i;
}
sort(r + 1, r + m + 1);
//对询问进行双关键字排序(以区间右端为第一关键字)

for (int i = 1, j = 1; i <= n; i++)
{
x = b[i]; p[x].push_back(i);
y = p[x].size() - 1;
if (y >= k)
{
add(p[x][y - k] + 1, 1);
add(p[x][y - k + 1] + 1, -1);
if (y > k)
{
add(p[x][y - k - 1] + 1, -1);
add(p[x][y - k] + 1, 1);
}
}
for (; j <= m&&r[j].c == i; j++) ans[r[j].a] = sum(r[j].b);
}
//关键操作:树状数组累加和,p[i][j]代表了值i第j次出现的位置。

if (t) cout << endl;
cout << "Case #" << t + 1 << ":" << endl;
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
return 0;
}