Problem of the day
Let ‘N’ = 5, ‘EDGES’ = [[1, 2], [1, 3], [2, 4], [2, 5]], ‘QUERIES’ = [[1, 2, 3], [2, 1], [1, 3, 2], [2, 1]] and ‘COLOR’ = [1, 1, 1, 1, 1].
The initial tree will be:
After processing the query [1, 2, 3] i.e. colour all vertices in the subtree of 2 to 3.
Now the number of different colours in subtree of the vertex 1 is 2.
After processing the query [1, 3, 2] i.e. colour all vertices in the subtree of 3 to 2.
Now the number of different colours in subtree of the vertex 1 are 3.
Hence the answer to this test case is 2 and 3 (print on separate lines).
The first line contains an integer 'T', which denotes the number of test cases.
The next line contains two integers ‘N’ and ‘Q’ denoting the number of vertices and the number of queries respectively.
The next line contains ‘N’ space-separated integers, ‘COLOR[i]’ denoting the initial colour of i-th vertex.
Each of the next N - 1 line contains two integers Uj, Vj (1 ≤ Uj, Vj ≤ N) - the vertices of j-th edge.
The last ‘Q’ lines contain the description of the queries. Each description starts with the integer Tk (1 ≤ Tk ≤ 2) - the type of k-th query.
For the queries of the first type then follows two integers Vk, Ck (1 ≤ Vk ≤ N, 1 ≤ Ck ≤ 60) - the number of the vertex whose subtree will be recoloured with the colour Ck.
For the queries of the second type then follows integer Vk(1 ≤ Vk ≤ N) - the number of the vertex for which subtree you should find the number of different colours.
For each test case, return an array ‘ANS’, where ‘ANS[i]’ will denote the answer to i-th query of the second type.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
1 ≤ Q ≤ 1000
1 ≤ COLOR[i] ≤ 60
Time limit: 1 sec
2
5 4
1 1 1 1 1
1 2
1 3
2 4
2 5
1 2 3
2 1
1 3 2
2 1
3 2
1 1 1
1 2
1 3
1 2 2
2 1
2
3
2
Test Case 1:
The initial tree will be:
After processing the query [1, 2, 3] i.e. color all vertices in subtree of 2 to 3.
Now the number of different colors in subtree of the vertex 1 are 2.
After processing the query [1, 3, 2] i.e. color all vertices in subtree of 3 to 2.
Now the number of different colors in subtree of the vertex 1 are 3.
Hence the answer to this test case is 2 and 3 (print on separate lines).
Test Case 2:
The initial tree will be:
After processing the query [1, 2, 2] i.e. color all vertices in subtree of 2 to 2.
Now the number of different colors in the subtree of the vertex 1 are 2.
Hence the answer to this test case is 2.
2
2 2
1 3
1 2
1 1 2
2 1
1 1
1
2 1
1
1
Test Case 1:
The initial tree will be:
After processing the query [1, 1, 2] i.e. color all vertices in the subtree of 1 to 2.
Now the number of different colors in subtree of vertex 1 is 1. Hence answer to this testcase will be 1.
Test Case 2:
Initially, tree will be:
The number of different colors in the subtree of vertex 1 is only 1. Hence answer to this testcase will be 1.