Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve this question. And try to explain step by step the solution. Thanks in advance. You are given a tree with each vertex coloured

Please solve this question. And try to explain step by step the solution.

Thanks in advance.

You are given a tree with each vertex coloured white, black or grey. You can remove elements from the tree by selecting a subset of vertices in a single connected component and removing them and their adjacent edges from the graph. The only restriction is that you are not allowed to select a subset containing a white and a black vertex at once.

What is the minimum number of removals necessary to remove all vertices from the tree?

Input

Each test contains multiple test cases. The first line contains an integert

t(1t100000

1t100000), denoting the number of test cases, followed by a description of the test cases.

The first line of each test case contains an integern

n(1n200000

1n200000): the number of vertices in the tree.

The second line of each test case containsn

nintegersa

v

av(0a

v

2

0av2): colours of vertices. Gray vertices havea

v

=0

av=0, white havea

v

=1

av=1, black havea

v

=2

av=2.

Each of the nextn1

n1lines contains two integersu,v

u,v(1u,vn

1u,vn): tree edges.

The sum of alln

nthroughout the test is guaranteed to not exceed200000

200000.

Output

For each test case, print one integer: the minimum number of operations to solve the problem.

Example

input

Copy

4 2 1 1 1 2 4 1 2 1 2 1 2 2 3 3 4 5 1 1 0 1 2 1 2 2 3 3 4 3 5 8 1 2 1 2 2 2 1 2 1 3 2 3 3 4 4 5 5 6 5 7 5 8 

output

Copy

1 3 2 3 

Note

In the first test case, both vertices are white, so you can remove them at the same time.

In the second test case, three operations are enough. First, we need to remove both black vertices (2 and 4), then separately remove vertices 1 and 3. We can't remove them together because they end up in different connectivity components after vertex 2 is removed.

In the third test case, we can remove vertices 1, 2, 3, 4 at the same time, because three of them are white and one is grey. After that, we can remove vertex 5.

In the fourth test case, three operations are enough. One of the ways to solve the problem is to remove all black vertices at once, then remove white vertex 7, and finally, remove connected white vertices 1 and 3.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions