【Codeforces Round #551(Div. 2)】Serval and Rooted Tree(思维+dp)

题目链接

D. Serval and Rooted Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 vvvertex 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 maxmax or minmin 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,…,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?

Input

The first line contains an integer nn (2≤n≤3⋅1052≤n≤3⋅105), 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 minmin and 11 represents maxmax. 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,…,fnf2,f3,…,fn (1≤fi≤i−11≤fi≤i−1), where fifi represents the parent of the node ii.

Output

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

Examples

input

Copy

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

output

Copy

1

input

Copy

5
1 0 1 0 1
1 1 1 1

output

Copy

4

input

Copy

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

output

Copy

4

input

Copy

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

output

Copy

5

【题意】

给一个树形结构,每个节点定义了取最大值或最小值,叶子节点可以取1-k的数字(k为叶子节点数),输出使根节点最大。

【解题思路】

设dp[i]为以i为根的子树中叶子节点的数值中排名第dp[i]大的数值。这句话有点难理解,但是如果看代码,就比较容易明白了。

所以最终答案为5+1-1=5

即如果该节点需要最小值,那么将其儿子节点的数都加起来,因为最小值的话是取其儿子节点中最小的那个,最大值则相反。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
const int INF=0x3f3f3f3f;
int vis[maxn],a[maxn],dp[maxn];
vector<int>v[maxn];
void dfs(int x)
{
    if(v[x].size()==0)
    {
        dp[x]=1;
        return;
    }
    if(a[x])
    {
        dp[x]=INF;
        for(int i=0;i<v[x].size();i++)
        {
            dfs(v[x][i]);
            dp[x]=min(dp[x],dp[v[x][i]]);
        }
    }
    else
    {
        for(int i=0;i<v[x].size();i++)
        {
            dfs(v[x][i]);
            dp[x]+=dp[v[x][i]];
        }
    }
}
int main()
{
    int n,cnt=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
    {
        int u;
        scanf("%d",&u);
        v[u].push_back(i);
        vis[u]=1;
    }
    for(int i=1;i<=n;i++)
        if(!vis[i])cnt++;
    dfs(1);
    printf("%d\n",cnt+1-dp[1]);

}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/89358426