CF1153D.Serval and Rooted Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

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?

现在,Serval 是加帕里中学的一名初中生,他对数学的热情一如往昔。

作为一名数学天赋出众的少年,他喜欢与数字打交道。这一次,他想在一棵有根树上玩数字游戏。

树是一种无环的连通图。有根树具有一个特殊的顶点,称为根(root)。节点 vv 的父节点(parent)是指从根到 vv 的路径上,最后一个不同于 vv 的顶点。若节点 vv 是某节点的父节点,则该节点称为 vv 的子节点(child)。若一个顶点没有子节点,则称其为叶子节点(leaf)。

Serval 所拥有的这棵有根树共有 nn 个节点,其中节点 11 为根。Serval 将在树的所有节点中填入一些数字,但需满足如下限制:除叶子节点外,每个节点均标有一个运算符 max⁡\max 或 min⁡\min,表示该节点中的数值应等于其所有子节点中数值的最大值或最小值。

假设该树中共有 kk 个叶子节点。Serval 希望将整数 1,2,…,k1, 2, \ldots, k 恰好各使用一次,填入这 kk 个叶子节点中。他偏爱较大的数,因此希望使根节点中的数值尽可能大。作为他最好的朋友,你能帮他实现这一目标吗?

输入格式

The first line contains an integer nn (2≤n≤3⋅1052 \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 n−1n-1 integers f2,f3,…,fnf_2, f_3, \ldots, f_n (1≤fi≤i−11 \leq f_i \leq i-1), where fif_i represents the parent of the node ii.

第一行包含一个整数 nn(2≤n≤3⋅1052 \leq n \leq 3\cdot 10^5),表示树的大小。

第二行包含 nn 个整数,其中第 ii 个数表示节点 ii 上的操作:00 表示 min⁡\min,11 表示 max⁡\max。若该节点为叶子节点,则对应位置仍为 00 或 11,但可忽略其含义。

第三行包含 n−1n-1 个整数 f2,f3,…,fnf_2, f_3, \ldots, f_n(1≤fi≤i−11 \leq f_i \leq i-1),其中 fif_i 表示节点 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.

下方图片解释了各个示例。节点中间的数字表示其索引,顶部的数字表示写在该节点上的数值。

在第一个示例中,无论怎样安排这些数字,答案均为 11。

在第二个示例中,无论怎样安排这些数字,答案均为 44。

在第三个示例中,一种达到 44 的最优方案是将 44 和 55 分别安排至节点 44 和节点 55。

在第四个示例中,最优方案是将 55 安排至节点 55。

输入解题思路,AI测评打分。不知道怎么写?

首页