Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There is an undirected tree where each vertex is numbered from 1 to n , and each contains a data value. The sum of a
There is an undirected tree where each vertex is numbered from to n and each contains a data value. The sum of a tree is the sum of all its nodes' data values. If an edge is cut, two smaller trees are formed. The difference between two trees is the absolute value of the difference in their sums.
Given a tree, determine which edge to cut so that the resulting trees have a minimal difference between them, then return that difference.
Example
data
edges
In this case, node numbers match their weights for convenience. The graph is shown below.
The values are calculated as follows:
Edge Tree Tree Absolute
Cut Sum Sum Difference
The minimum absolute difference is
Note: The given tree is always rooted at vertex
Function Description
Complete the cutTheTree function in the editor below.
cutTheTree has the following parameters:
int datan: an array of integers that represent node values
int edgesn: an dimensional array of integer pairs where each pair represents nodes connected by the edge
Returns
int: the minimum achievable absolute difference of tree sums
Input Format
The first line contains an integer n the number of vertices in the tree.
The second line contains n spaceseparated integers, where each integer u denotes the nodeu data value, datau
Each of the n subsequent lines contains two spaceseparated integers u and v that describe edge u v in tree t
Constraints
n
datau where un
Sample Input
STDIN Function
data size n
data
edges
Sample Output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started