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 v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all nodes for which v is the parent. A vertex is a leaf if it has no children.
The rooted tree Serval owns has n nodes, node 1 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 or 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 k leaves in the tree. Serval wants to put integers 1,2,…,k to the k 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 n
(
2
≤
n
≤
3
⋅
1
0
5
)
(2≤n≤3⋅10^5)
(2≤n≤3⋅105), the size of the tree.
The second line contains n integers, the i-th of them represents the operation in the node i. 0 represents min and 1 represents max. If the node is a leaf, there is still a number of 0 or 1, but you can ignore it.
The third line contains n−1 integers f 2 , f 3 , … , f n ( 1 ≤ f i ≤ i − 1 ) f_2,f_3,…,f_n (1≤f_i≤i−1) f2,f3,…,fn(1≤fi≤i−1), where f i f_i fi represents the parent of the node i.
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
思路:对于每棵子树来说,根节点的值越大越好,因为有操作符的存在,无法准确求出根结点的值,但可以求出根节点的值在这个子树所有叶节点的值中大小排第几。
用 d [ i ] d[i] d[i]表示以 i i i为根节点的子树中,根节点 i i i所得到的在子树中排名的最大值。
当 a [ i ] = 0 a[i]=0 a[i]=0时, d [ i ] = 1 + ∑ ( d [ j ] − 1 ) , j ∈ i 的 儿 子 d[i]=1+\sum( d[j]-1),j\in i的儿子 d[i]=1+∑(d[j]−1),j∈i的儿子,即调整所有节点的大小,使儿子中排名最小的在子树 i i i中的排名尽量大。
当 a [ i ] = 1 a[i]=1 a[i]=1时, d [ i ] = m a x ( 除 j 子 树 以 外 所 有 的 叶 子 个 数 + d [ j ] ) , j ∈ i 的 儿 子 d[i]=max(除j子树以外所有的叶子个数+d[j]),j\in i的儿子 d[i]=max(除j子树以外所有的叶子个数+d[j]),j∈i的儿子,及把其它儿子的值全部看成小于 j j j子树的值,使儿子中排名最大的在子树 i i i中的排名尽量大。
#include<bits/stdc++.h>
using namespace std;
const int MAX=3e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
vector<int>e[MAX];
int d[MAX],a[MAX];
int siz[MAX];
void dfs(int k)
{
d[k]=siz[k]=0;
if(e[k].size()==0){d[k]=1,siz[k]=1;return;}
int sum=0;
for(int i=0;i<e[k].size();i++)
{
dfs(e[k][i]);
siz[k]+=siz[e[k][i]];
sum+=d[e[k][i]]-1;
}
if(a[k]==0){d[k]=max(d[k],sum+1);return;}
for(int i=0;i<e[k].size();i++)d[k]=max(d[k],siz[k]-siz[e[k][i]]+d[e[k][i]]);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=2;i<=n;i++)
{
int x;
scanf("%d",&x);
e[x].push_back(i);
}
dfs(1);
cout<<d[1]<<endl;
return 0;
}