Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a tree with N nodes with some non-negative value assigned to it. Lets say that the value of the ith node is wi. The

Given a tree with N nodes with some non-negative value assigned to it. Lets say that the value of the ith node is wi. The beauty of a path between nodes u and v (denoted by B(u,v)) is described as the sum of values of all nodes that lie on the path excluding the nodes u and v. Note that B(u,u) is always zero for any valid node u. Ash is a weird guy. He only likes trees that have Nu=1Nv=1 B(u,v)=0. In one operation Ash can change the value of any node to any non-negative value. Ash wants to find the minimum number of operations required so that he starts liking the given tree. But Ash does not have time to solve the problem as he is on his Pokemon adventure. So he needs your help in solving this problem.

Input Format:

The first line contains an integer T denoting the number of test cases. (1T10)

The following format is followed for each test case:

Next line contains a single integer N denoting the number of nodes in the tree. (1N10^5)

Next N1 lines contains two space-separated integers u and v denoting that there is an edge between u and v. (1u,vN)

Next line contains N space-separated integers where the ith integers denotes wi. (0wi10^9)

Output Format:

For each test case, output the minimum number of operations required on a new line.

SAMPLE INPUT

1

3

1 2

1 3

1 2 3

SAMPLE OUTPUT

1

Explanation

Following are the beauties of all paths:

B(1,2)=0, B(1,3)=0, B(2,3)=1.

We can change the value of node 1 and thus, beauties of all the paths will become 0. So, answer will be 1.

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions