#P3237. [HNOI2014] 米特运输
[HNOI2014] 米特运输
Description
Mite (mi te) is a very mysterious substance on planet D, containing huge amounts of energy. On planet D, which uses mite as its main energy source, transporting and storing this mite energy has always been a major problem.
There are cities on planet D, numbered through , with city being the capital. These cities are connected by one-way expressways, forming a directed tree rooted at city (the capital). The direction of each expressway is from a child to its parent. The tree is layered by depth: the root has depth and belongs to layer ; a child of the root has depth and belongs to layer ; and so on, a node of depth belongs to layer .
After building the expressways, the D people began to consider how to store and transmit mite. Due to differing levels of development, each city has a different storage capacity. City has a mite storage unit with capacity . Besides storing, a storage unit can also automatically collect mite from the atmosphere.
If, at 6 pm, some storage unit is not full, it will automatically collect mite from the atmosphere and will be filled before 6 am the next day; however, it is only safe to start this auto-collection when the storage unit is completely empty. Starting auto-collection when a storage unit is non-empty but not full may cause safety hazards.
Between 6 am and 7 am, the capital (city ) consumes all mite in its storage unit. The root does not auto-collect mite; it only accepts mite transmitted from its children.
At 7 am, cities start the transmission process, proceeding layer by layer: first, cities in layer transmit to layer (the root, city ), until the root’s storage is full or all layer- storage units are empty; then layer transmits to layer , ensuring that for every node in layer , its storage becomes full or all of its children’s (in layer ) storage units become empty; and so on, until the last layer completes transmission. The transmission process will certainly finish before 6 pm.
Due to technical reasons, the transportation plan must satisfy the following conditions:
- No storage unit may remain non-empty but not full at 6 pm when transmission ends. Otherwise, it would start auto-collection while already containing mite, which is dangerous. In other words, by 6 pm, every storage unit must be either empty or full.
- For the capital (city ), every day between 6 am and 7 am its storage unit is automatically emptied. The transportation plan does not need to consider how to move mite out of the capital.
- Except for city , each node must first send out all the mite originally stored in its own storage unit to its parent before its children transmit mite to it. Mixing locally stored mite with incoming mite is not allowed.
- For any city, the amounts of mite sent to it from its multiple sources must be exactly the same. Otherwise, mixing different sources in different proportions may be dangerous.
Now that the expressways are built and each city has a storage unit with some capacity, to meet the above constraints, it may be necessary to rebuild some storage units. You may, and only may, destroy the existing storage unit in a city (including the capital) and build a new one with any positive capacity. The new capacity may be a real number (in the input, original capacities are positive integers, but rebuilt capacities may be fractional), and it cannot be negative or zero. Your goal is to minimize the number of storage units that need to be rebuilt.
Input Format
The first line contains a positive integer , the number of cities.
The next lines each contain a positive integer; line gives the original capacity of the storage unit in city .
Then follow lines, each containing two positive integers , indicating there is a one-way expressway from city to city ().
Output Format
Output a single line with one integer: the minimum number of storage units that must be rebuilt (i.e., whose capacities are modified).
5
5
4
3
2
1
1 2
1 3
2 4
2 5
3
Hint
Sample explanation: One optimal solution is to change to , to , and to . Then the amounts sent from and to are equal, the amounts sent from and to are equal, and at 6 pm every day, and are full while , , and are empty, satisfying all constraints.
Constraints: For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号