Question: A mathematician is attempting to solve a math problem. In this problem, they are given a tree with N nodes, indexed from 1 to N

A mathematician is attempting to solve a math problem. In this problem, they are given a tree with N nodes, indexed from 1 to N, where the root node is indexed as 1. Each node of the tree has a defined value. They want to trace a path from one leaf to another leaf in such a way that will award them the maximum score for that path. The score of a path is defined as the product of node values along the path. Write an algorithm to find the maximum possible score. Input The first line of the input consists of an integer - num, representing the number of nodes in the tree (N). The second line consists N space-separated integers representing the value of each node in the tree. The third line consists of two space-separated integers - numEdges and numNodes, representing the number of edges (E) and the number of nodes forming an edge (where numNodes (V)=2 always), respectively. The next E lines consist of two space-separated integers - start and end, representing the indices of the starting node and ending node of an edge of the tree. Output Print an integer representing the maximum possible score. Constraints 1 num 103-103 val 103 ; where val is the value of a node numEdges = num-1 numNodes =2 Example Input: 4-123232121334 Output: -12 Explanation: 1(-1)/\2(2)3(3)/4(2) There is only one route from leaf 2 to leaf 4, as there are only 2 leaves. (2)->(1) : Score =2*-1=-2(2)->(1)->(3): Score =-2*3=-6(2)->(1)->(3)->(4):Score =-6*2=-12 So best possible answer is -12.

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!