Codeforces Round 551 (Div. 2)
D
Serval and Rooted Tree
Codeforces
1900
原题
← CE →
翻译中…

Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before.

翻译中…

As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.

翻译中…

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node $v$ is the last different from $v$ vertex on the path from the root to the vertex $v$. Children of vertex $v$ are all nodes for which $v$ is the parent. A vertex is a leaf if it has no children.

翻译中…

The rooted tree Serval owns has $n$ nodes, node $1$ is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation $\max$ or $\min$ written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively.

翻译中…

Assume that there are $k$ leaves in the tree. Serval wants to put integers $1, 2, \ldots, k$ to the $k$ leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?

翻译中…

Input

翻译中…

The first line contains an integer $n$ ($2 \leq n \leq 3\cdot 10^5$), the size of the tree.

翻译中…

The second line contains $n$ integers, the $i$-th of them represents the operation in the node $i$. $0$ represents $\min$ and $1$ represents $\max$. If the node is a leaf, there is still a number of $0$ or $1$, but you can ignore it.

翻译中…

The third line contains $n-1$ integers $f_2, f_3, \ldots, f_n$ ($1 \leq f_i \leq i-1$), where $f_i$ represents the parent of the node $i$.

翻译中…

Output

翻译中…

Output one integer — the maximum possible number in the root of the tree.

翻译中…

Note

翻译中…

Pictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes.

翻译中…

In the first example, no matter how you arrange the numbers, the answer is $1$.

翻译中…

image

翻译中…

In the second example, no matter how you arrange the numbers, the answer is $4$.

翻译中…

image

翻译中…

In the third example, one of the best solution to achieve $4$ is to arrange $4$ and $5$ to nodes $4$ and $5$.

翻译中…

image

翻译中…

In the fourth example, the best solution is to arrange $5$ to node $5$.

翻译中…

image

翻译中…

样例

Input #1
6
1 0 1 1 0 1
1 2 2 2 2
Output #1
1
InputCopy OutputCopy
Input #2
5
1 0 1 0 1
1 1 1 1
Output #2
4
InputCopy OutputCopy
Input #3
8
1 0 0 1 0 1 1 0
1 1 2 2 3 3 3
Output #3
4
InputCopy OutputCopy
Input #4
9
1 1 0 0 1 0 1 0 1
1 1 2 2 3 3 4 4
Output #4
5
InputCopy OutputCopy
题目信息
题号
1153D
时间限制
2 s
内存限制
256 MB
标签
二分
DFS 及类似
动态规划
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。