Codeforces 1153 D Serval and Rooted Tree

http://codeforces.com/problemset/problem/1153/D

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

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

Output

1

Input

5
1 0 1 0 1
1 1 1 1

Output

4

Input

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

Output

4

Input

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

Output

5

题目大意:给出一颗树,树有k个叶子节点,你可以给其赋任何一个[1,k]之间的值,但每一个值只能赋一次,其余节点的值为0或1,0代表该节点的值是该节点的孩子节点的所有值中的最小值,1代表该节点的值是该节点的孩子节点的所有值中的最大值。问根结点的值最大是多少。

思路:树形dp。dp还是练得少,稍微难一点就不会了,大坑啊orz。其实这道题可以换一种思维来理解:我们设这棵树一共有k个叶子节点,节点A有t个叶子节点与其相连,则节点A的最大值等于k-t+1(不论哪种操作),因此我们考虑计算根结点的最小的t值,也可以得出答案。对于叶子节点i,cnt[i]=1;对于其余节点,若a[i]=1,即做取最大值的操作,设j为i的子节点,则cnt[i]=min(cnt[j]);若a[i]=0,即做取最小值的操作,设j为i的子节点,此时我们没有选择的余地,即cnt[i]=∑cnt[j]

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std ;

vector<int> vec[300005];
int a[300005];
int cnt[300005];
int sum;

void dfs(int cur)
{
    if(vec[cur].size()==0)//叶子节点
    {
        ++sum;
        cnt[cur]=1;
        return ;
    }
    int tot=0;
    for(int i=0;i<vec[cur].size();i++)
    {
        dfs(vec[cur][i]);
        if(a[cur])//max
            cnt[cur]=min(cnt[cur],cnt[vec[cur][i]]);
        tot+=cnt[vec[cur][i]];
    }
    if(!a[cur])
        cnt[cur]=tot;
    return ;
}

int main()
{
    memset(cnt,INF,sizeof(cnt));
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int t;
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&t);
        vec[t].push_back(i);
    }
    dfs(1);
    printf("%d\n",sum-cnt[1]+1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/89356129
今日推荐