As we all know, frogs live on trees and have different colors.
N frogs are living on a tree. The tree consists of N nodes with node 1 as the root, each frog occupies
a node.
Frogs have different colors, and can change colors as they like. On each day, all the frogs living on
a certain sub-tree will change its color. The root of the sub-tree, and the color they change to, is given
to the frog king.
As the frog king, sometimes he may wonder, how many different colors of frog are there in a certain
sub-tree? It turns to you to solve the problem for the king.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case begins with an integers N, which is the numbers of nodes in the tree.
The following N − 1 lines describe the edges of the tree, and every line is formatted as ‘u v’, which
indicates there is a edge between node u and node v.
The next line contains N intergers, c1, c2, · · ·, cN , and ci
is the initial color of the frog living at
node i.
Then a number M follows, which indicates the number of queries, and following M lines describe
the quries as format bellow.
operation format description
modify color 0 u c change the color of all frogs in the sub-tree rooted at
node u to c
query 1 u query how many different colors of frog are there in
the sub-tree rooted at node u
Restrictions:
• 1 ≤ T ≤ 100.
• For 85% data, 1 ≤ N, M ≤ 1000.
• for 100% data, 1 ≤ N, M ≤ 105
.
• for every node, 1 ≤ ci ≤ N.
• for every edge, 1 ≤ u, v ≤ N.
• for every query, 1 ≤ u, c ≤ N.
Output
For every test case, you should output ‘Case #x:’ first, where x indicates the case number and counts
from 1.
Then for each query operation, output the number of different colors.
Sample Input
1
5
1 2
1 3
2 5
2 4
1 2 3 4 5
6
1 1
0 4 2
1 1
1 2
0 2 3
1 2
Sample Output
Case #1:
5
4
2
1
原题传送门
WC上刘汝佳先生推荐的题,当时和许多小伙伴想了许久,有人给出了一个好像还不错的做法(然而忘了……)后来给刘发了邮件询问,它给我发了一篇题解,看了之后总算大概明白该怎么做了。通过这个题真的是明白了 树上做的事情比起直接在序列上做,不见得更糟糕。 (想引用WC上营员交流时……好像是小火车说的话?“现在有些出题人,总喜欢把序列上的问题出到树上,让选手强行写个树链剖分……其实,出到仙人掌上也是可以的……希望大家不要拿这种东西出题……”),zgy神犇的一句话很好地解释了这个看起来不太合理的事实背后的原因:“子树只有n个(区间有 O(n2)个