Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

What online recruitment methods are available?

Answered: 1 week ago