Question: Problem D ( answer in python , C# or javascript ) In the Favela of Rio de Janeiro there was a flicker of light. After
Problem D answer in python C# or javascript
In the Favela of Rio de Janeiro there was a flicker of light. After months of careful building, they finally connected the generator to the thousands of extension cords and the slum was illuminated by millions of bright lights...
However, the capacity of the extension cords was not enough to meet the energy demands of all the houses in the slum. Thus, the engineers had to carefully select which houses should be
Photo by Joe Friedrichsen
powered, and which should not, prior to connecting the power generator. Their idea was to power as many houses as possible, based on the energy demands of each house, and the capacities of the extension cords.
More specifically, the generator and all the houses are represented by nodes, and extension cords are represented by directed edges between them. Furthermore, every house node i gets power from exactly one other node and has a nonnegative power demand You can assume that each house can get power from the generator either directly or via other houses.
The flow through an extension cord is the sum of the energy demands that are met in the subtree rooted at and this flow can not exceed the capacity of the extension cord. A node which does not have its energy demand satisfied can still have some of its children satisfied. The generator produces an amount of energy that surpasses by far the total capacity of the extension cords connected to it so can be treated as an infinite energy source.
Given the same data as the engineers, find out how many houses the engineers were able to cover the energy demands of
Input
The first line of the input contains a single integer the number of houses in the slum. lines follow in the format where is the parent node of house is the energy demand of house i and is the capacity of the extension cord connecting house to house The power generator has index
Output
Output the maximum number of power requirements that can be met.
Sample Input
Sample Output
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
