CF1153D.Serval and Rooted Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 vv is the last different from vv vertex on the path from the root to the vertex vv . Children of vertex vv are all nodes for which vv is the parent. A vertex is a leaf if it has no children.

The rooted tree Serval owns has nn nodes, node 11 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\max or min\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 kk leaves in the tree. Serval wants to put integers 1,2,,k1, 2, \ldots, k to the kk 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?

输入格式

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

The second line contains nn integers, the ii -th of them represents the operation in the node ii . 00 represents min\min and 11 represents max\max . If the node is a leaf, there is still a number of 00 or 11 , but you can ignore it.

The third line contains n1n-1 integers f2,f3,,fnf_2, f_3, \ldots, f_n ( 1fii11 \leq f_i \leq i-1 ), where fif_i represents the parent of the node ii .

输出格式

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

输入输出样例

  • 输入#1

    6
    1 0 1 1 0 1
    1 2 2 2 2
    

    输出#1

    1
    
  • 输入#2

    5
    1 0 1 0 1
    1 1 1 1
    

    输出#2

    4
    
  • 输入#3

    8
    1 0 0 1 0 1 1 0
    1 1 2 2 3 3 3
    

    输出#3

    4
    
  • 输入#4

    9
    1 1 0 0 1 0 1 0 1
    1 1 2 2 3 3 4 4
    

    输出#4

    5
    

说明/提示

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 11 .

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

In the third example, one of the best solution to achieve 44 is to arrange 44 and 55 to nodes 44 and 55 .

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

首页