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 pi and has a nonnegative power demand ri. You can assume that each house can get power from the generator either directly or via other houses.
The flow through an extension cord (pi,i) is the sum of the energy demands that are met in the subtree rooted at i, and this flow can not exceed the capacity ci 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 0N1000, the number of houses in the slum. N lines follow in the format pirici, where 0piN is the parent node of house i,0ri100 is the energy demand of house i and 1ci100 is the capacity of the extension cord connecting house i to house pi. The power generator has index 0.
Output
Output the maximum number of power requirements that can be met.
Sample Input 1
Sample Output 1
3
0.2
0,100,100
111
 Problem D (answer in python , C# or javascript) In the

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!