Question: Please Solve this problem in JAVA! A tree can be represented as an unweighted undirected graph of t_nodes nodes numbered from 1 to tnodes and

Please Solve this problem in JAVA!

Please Solve this problem in JAVA! A tree can be represented as

an unweighted undirected graph of t_nodes nodes numbered from 1 to tnodes

and t_nodes - 1 edges where the ith edge connects the nodes

numbered tfrom [i] and tto[i]. A value val[i] is associated with the

ith node. In a single operation, two nodes can be selected, and

A tree can be represented as an unweighted undirected graph of t_nodes nodes numbered from 1 to tnodes and t_nodes - 1 edges where the ith edge connects the nodes numbered tfrom [i] and tto[i]. A value val[i] is associated with the ith node. In a single operation, two nodes can be selected, and their values can be decremented by 1 at a cost equal to the distance between the two nodes, i.e., the number of edges in the simple path between them. It is possible to select the same node for the operation and decrease its value by 2 at the cost of 0 . Given the tree, t, and the values val, find the minimum cost to decrease the values of all the nodes to 0 . It is guaranteed that the values of all the nodes can be decreased to exactly 0 . Example Suppose tnodes =3,tfrom =[1,1],tto =[2,3], and val=[3,1,2]. The optimal strategy is to choose nodes 1 and 2 at cost 1 . The values become [2,0,2]. Now nodes 1 and 1 , followed by 3 and 3 , can be chosen, each with a cost of 0 . Thus the total cost is 1+0+0 =1. Function Description Complete the function getMinCost in the editor below. getMinCost has the following parameters: int val[t_nodes]: the values of the nodes int t_nodes. the number of nodes in the tree int t from[t_nodes - 1]: an end of an edge int t_to[t_nodes - 1]: an end of an edge Returns int: the minimum possible cost to decrease all the values to 0 Constraints - 2tnodes 105 - 1tfrom [i],tto [i]tnodes - 0val[i]100 - The input guarantees that there always exists a way to pair all the elements in val. Input Format For Custom Testing The first line contains an integer, t_nodes, which denotes the number of elements in val. Each line i of the n subsequent lines (where 0i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!