#P3237. Minimal Storage Rebuilds in D Planet
Minimal Storage Rebuilds in D Planet
Minimal Storage Rebuilds in D Planet
D planet uses a mysterious energy material called "Mitt" stored in special reservoirs. There are N cities numbered from 1 to N, with city 1 the capital. The cities are connected by N-1 one‐directional high-speed corridors forming a tree with edges pointing from children to parent.
Each city i has a reservoir with initial capacity Ai (a positive integer). Every night, if a reservoir is not full and is completely empty at 6 pm, it can safely collect Mitt from the atmosphere and be full by 6 am. However, if a reservoir is partially full (non‐empty and not full) at 6 pm, starting automatic collection is unsafe.
Every morning between 6 and 7, the capital’s reservoir is emptied (its Mitt is consumed). Then at 7 am, the cities start a layer‐by‐layer transmission process. Specifically: first the cities in layer 2 send all the Mitt stored in their reservoirs to city 1 (the capital), until either city 1’s reservoir is full or the reservoirs of layer‑2 cities become empty; then the cities in layer 3 send their entire stored Mitt to their respective parent city in layer 2, but each city must first send out its originally stored Mitt before receiving any transmissions from its children. This process continues layer by layer. In addition, all Mitt coming from different children to a single parent must be in equal amounts to avoid dangerous mixing.
Because of technical reasons, the transportation (i.e. transmission) plan must satisfy these conditions:
- At 6 pm, every reservoir must be either completely empty or exactly full (a partially filled reservoir will automatically start collection, which is dangerous if it already holds some Mitt).
- The capital (city 1) automatically empties its reservoir between 6 and 7 am so its Mitt does not need to be moved.
- Except for city 1, every city must send out all the Mitt originally stored in its reservoir before receiving transmissions from its children (no mixing is allowed).
- The amounts of Mitt transported from any two different sources to the same city must be exactly the same.
To meet these strict conditions, some reservoirs may need to be rebuilt. You cannot partially adjust a reservoir – you must destroy the original reservoir and rebuild it with an arbitrarily chosen positive real capacity (possibly fractional) if you want to modify it. Rebuilding is expensive, so your goal is to minimize the number of reservoirs that need to be rebuilt (this decision applies to every city including the capital).
Two transmission cases occur at every non‐leaf node (except that the capital does not transmit its own Mitt):
- If the city is not rebuilt, its capacity remains Ai, and in order to safely receive equal transmissions from its k children, the following must hold: each child must send an amount equal to Ai/k (so each child’s reservoir capacity must be exactly Ai/k).
- If the city is rebuilt, you can set its capacity arbitrarily – however, you must set it exactly equal to the required amount imposed by its parent. That is, if its parent requires this city to have capacity r, then after rebuilding the city’s capacity will be r.
The process is defined recursively from the capital (which, if it has children, must eventually receive transmissions that exactly fill its originally defined or rebuilt capacity) down to the leaves. Your task is to compute the minimum number of reservoirs that need to be rebuilt in order to have a safe and consistent transmission plan.
Note on automatic collection: A reservoir that is empty at 6 pm will safely collect Mitt to be full by 6 am; however, if there is any residual Mitt (i.e. partially filled), automatic collection might be triggered unsafely. Thus, at the end of the transmission process each reservoir (except the capital which is emptied) must be either completely empty or exactly full.
inputFormat
The first line contains an integer N (the number of cities).
The second line contains N positive integers A1 A2 ... AN, where Ai is the capacity of the reservoir in city i.
Then N-1 lines follow. For each i from 2 to N, the (i+1)-th line contains an integer p (1 ≤ p ≤ N) meaning that there is a one‐directional corridor from city i to city p (i.e. p is the parent of city i in the tree). It is guaranteed that these corridors form a tree with city 1 as the root.
outputFormat
Output a single integer – the minimum number of reservoirs that need to be rebuilt.
sample
1
10
0