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

18 篇文章 0 订阅
12 篇文章 0 订阅

传送门:https://codeforces.com/contest/1153/problem/D

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

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

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

给一颗树,并且存在k个叶子节点,然后对所有叶子节点赋值1到k之间的数字,不能重复,忽略叶子节点上的 0 1权值,

而普通节点1就是对当前节点的所有孩子节点求最大值赋值,0就是最小值,最后要求可以使得1节点的值最大

 

 

可以看出来是个树形dp结构,f[i]代表i节点下的子树,需要使用多少个节点,即表示节点i的权值在这个子树的所有叶节点中最小排名,那1节点的答案最大就是 k - f[1] + 1

转移:

当前节点如果权值是1,说明要取最大值,那我们就要选一个最小的排名来更新父亲节点,所以对他下面的节点求个min

如果是0,说明要取最小值那就保证当前节点排名最靠后,所以就是对他下面所有节点求sum,就是下面所有节点的最小排名

这样就结束了

代码如下:

 


ll c,n,k;
vector<int>v[maxn];
int f[maxn];
int a[maxn];
void dfs(int x,int fa){
    if(v[x].size() == 0) {
        k++,f[x] = 1;
        return;
    }
    if(a[x]) f[x] = 1e9;
    else f[x] = 0;
    for(auto d:v[x]){
        dfs(d,x);
        if(a[x])  f[x] = min(f[x],f[d]);
        else f[x] += f[d];
    }
}
int main() {
    ios::sync_with_stdio(false);
    while(cin >> n){
        for(int i = 1;i <= n;i++) cin >> a[i];
        for(int i = 2;i <= n;i++){
            int x; cin >> x;
            v[x].push_back(i);
        }
        dfs(1,0);
        cout << k - f[1] + 1<< endl;
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}





 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值