原题链接:http://codeforces.com/problemset/problem/1153/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.

题目大意:

给一棵树,树有min/max标记,设叶子节点数量为k,则让你分别给每个叶子节点赋[1, k]值 ,通过max/min计算后,使得根1节点的值最大。

解题思路:

权值是自己任意分配,观察可以发现,有些节点对根节点的值是没有贡献的,那么可以把问题看成对根节点有贡献的叶子节点个数。然后我们可以把[1, k]从大到小分配给这些叶子节点。贪心的想:如果根是max,那么肯定选择子树中叶子节点最少的;如果根是min,那么所有子树都要考虑了。dp[i] 为以 i 为根的子树叶子节点个数,如果 i 为 max,dp[i] = min { dp[i], dp[child[i][j]] }, 如果 i 为 min,dp[i] += dp[i][child[i][j]]; 答案为叶子节点总个数 all - dp[1] + 1.

AC代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;const int N = int(3e5 + 5);
const int INF = 0x3f3f3f3f;int n;
int o[N]; // operation
vector<int> tree[N];
int dp[N];
int all;void dfs(int r)
{if (tree[r].empty()){dp[r] = 1;all++;return;}if (o[r] == 1) dp[r] = INF;else dp[r] = 0;int i;for (i = 0; i < tree[r].size(); i++){dfs(tree[r][i]);if (o[r] == 1) dp[r] = min(dp[r], dp[tree[r][i]]);else dp[r] += dp[tree[r][i]];}
}int main()
{int i;while (scanf("%d", &n) != EOF){all = 0;for (i = 1; i <= n; i++){tree[i].clear();scanf("%d", &o[i]);}int f;for (i = 2; i <= n; i++){scanf("%d", &f);tree[f].push_back(i);}dfs(1);printf("%d\n", all - dp[1] + 1);}return 0;
}

Codeforces1153D-Serval and Rooted Tree(树形dp)相关推荐

  1. 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

    题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on ...

  2. 2021牛客多校4 - Rebuild Tree(树形dp)

    题目链接:点击查看 题目大意:给出一棵 nnn 个节点的树,现在可以删掉 kkk 条边,然后加上 kkk 条边,问有多少种方案使得操作后 nnn 个点仍然是一棵树 题目分析:原树删掉 kkk 条边后会 ...

  3. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  4. HDU 5834 Magic boy Bi Luo with his excited tree 树形DP

    Magic boy Bi Luo with his excited tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 Descript ...

  5. BZOJ.3227.[SDOI2008]红黑树tree(树形DP 思路)

    BZOJ orz MilkyWay天天做sxt! 首先可以树形DP:\(f[i][j][0/1]\)表示\(i\)个点的子树中,黑高度为\(j\),根节点为红/黑节点的最小红节点数(最大同理). 转移 ...

  6. LiberOJ #6210. 「美团 CodeM 决赛」tree 树形DP

    题目链接:点这里 题解: 需要证明,所求的路径一定是全部权值都为1或者,路径上权值至多有一个为2其余为1且权值2在路径中央. 然后树形DP 设定dp[i][0/1] 以1为根的情况下,以i 节点下子树 ...

  7. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

  8. VK Cup 2012 Round 1 D. Distance in Tree (树形dp)

    题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...

  9. ARC101E Ribbons on Tree 树形dp 容斥

    题目链接 题意: 给你一棵nnn个点的树,nnn是偶数,把这些点分成n2\frac{n}{2}2n​个点对,每个点对会把路径上的所有边覆盖,问你每条边至少覆盖一次有多少种配对方式.n<=5000 ...

  10. A. Parsa‘s Humongous Tree(树形DP + 贪心)

    Problem - 1528A - Codeforces 两个玩家正在玩一个游戏.他们有一个整数1,2,...,n的排列组合(排列组合是一个数组,其中从1到n的每个元素正好出现一次).这个排列组合没有 ...

最新文章

  1. MZOJ 1344 工作依赖
  2. 重装系统后sqlserver安装失败_Windows 10八月更新再遇尴尬:安装失败 或安装后随机重启...
  3. 继承Javadoc方法注释
  4. 『Balancing Act 树的重心』
  5. 程序员必修课:为什么非要用Python做数据分析?Excel不好吗?
  6. easyui combobox 左匹配模糊查询
  7. Titanium系列--对Window和View的一点理解
  8. 机器学习-1-.py文件的导入问题
  9. Unicode 汉字编码表
  10. Linux 系统日常巡检脚本
  11. HTML中的三目表达式可以有多长
  12. 如何使用计算机建模,计算机模拟在数学建模中的应用
  13. 蓝侠==la*uan,破解中国共享软件联盟著名灌水专家“蓝侠””
  14. 拍案惊奇——软件调试实战训练营暑期特别班(v2.1)
  15. LoRa模块无线通信技术在距离测量和定位上的应用——东胜物联
  16. 编程理念-程序基本编写IPO方法
  17. SCAU华南农业大学-数电实验-用74LS283实现2*4乘法器
  18. 启动电脑时出现0xc000000f错误的解决办法
  19. 超导材料应用于量子计算机,Nature子刊:超导超材料有望助力量子计算机的实现...
  20. Butterfly主题的应用

热门文章

  1. dcos - docker的日志收集
  2. jvav是什么梗?jvav是什么?jvav史上最牛语言
  3. courant数_CFD中常用的参数介绍 | 坐倚北风
  4. Vue 3 模板语法
  5. c语言程序设计 第四章 总结
  6. 单反相机的传奇—佳能单反50年辉煌之路(连载十五)
  7. solaris linux 计算磁盘容量 cyl alt sec
  8. 使用多种方法在Word方框中打对勾√和叉叉×
  9. ORAN C平面 Section Extension 10
  10. 计算机专硕毕业论文写什么,最新硕士毕业论文进度安排怎么写