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, \ldots, 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 \leq n \leq 3\cdot 10^5$), 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, \ldots, f_n$ ($1 \leq f_i \leq i-1$), where $f_i$ 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



-----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 $1$. [Image] 

In the second example, no matter how you arrange the numbers, the answer is $4$. [Image] 

In the third example, one of the best solution to achieve $4$ is to arrange $4$ and $5$ to nodes $4$ and $5$. [Image] 

In the fourth example, the best solution is to arrange $5$ to node $5$. [Image]

Difficulty: interview

Starter code:

Solutions:
import sys
from collections import deque
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
g = [[] for i in range(n)]
for i in range(n-1):
    g[b[i]-1].append(i+1)
ha = 0
for i in range(n):
    if len(g[i]) == 0:
        ha += 1
kyo = [1<<30] * n
def dfs(x,y):
  kyo[x] = y
  for i in g[x]:
    que.append([i,y+1])
que = deque()
que.append([0,0])
while que:
    aa,bb = que.popleft()
    dfs(aa,bb)
hukai = []
for i in range(n):
  hukai.append([kyo[i],i])
hukai.sort(key = lambda x:-x[0])
dp = [1<<30]*n
for j,i in hukai:
    if len(g[i]) == 0:
        dp[i] = 0
        continue
    sita = []
    for k in g[i]:
        sita.append(dp[k])
    if a[i] == 1:
        dp[i] = min(sita)
    else:
        dp[i] = sum(sita)+len(g[i])-1
ans = ha-dp[0]
print(ans)

from collections import defaultdict
from collections import deque
# import sys
# sys.setrecursionlimit(400000)
n = int(input())
tree = defaultdict(set)
node_type = list(map(int,input().split()))
#leaf = [True for x in range(n)]
values = [0 for x in range(n)]
for i,x in enumerate(map(int,input().split())):
    tree[x-1].add(i+1)
    #leaf[x-1] = False

queue = deque([(0,False)])
while queue:
    node, tag = queue.popleft()
    if not node in tree:
        values[node] = 1
        continue
    elif tag:
        if node_type[node]:
            values[node] = min(values[c] for c in tree[node])
        else:
            values[node] = sum(values[c] for c in tree[node])
    else:
        queue.appendleft((node,True))
        queue.extendleft((c,False) for c in tree[node])
#print(values)
k = n-len(tree)
print (k-values[0]+1)

n = int(input())

f = tuple(map(int, input().split()))

tree = [[] for _ in range(n)]
for i, parent in zip(list(range(1, n + 1)), [int(x) - 1 for x in input().split()]):
    tree[parent].append(i)

d = [0] * n

leaves = 0
for i in reversed(list(range(n))):
    if not tree[i]:
        d[i] = 1
        leaves += 1
        continue
    if f[i] == 0:
        d[i] = sum(d[j] for j in tree[i])
    else:
        d[i] = min(d[j] for j in tree[i])

# print(f)
# print(d)
print(leaves - d[0] + 1)


from collections import deque
N = int(input())
mima = [-1] + list(map(int, input().split()))
par = [-1, -1] + list(map(int, input().split()))
leaf = set(range(1, N+1))
cld = [0]*(N+1) 
for p in par:
    cld[p] += 1
    if p in leaf:
        leaf.remove(p)
dp = [0]*(N+1)
for i in leaf:
    dp[i] = 1

Q = deque(list(leaf))
visited = leaf.copy()
while Q:
    vn = Q.pop()
    if vn == 1:
        break
    pv = par[vn]
    if mima[pv] == 1:
        if not dp[pv]:
            dp[pv] = dp[vn]
        else:
            dp[pv] = min(dp[pv], dp[vn])
        cld[pv] -= 1
        if not cld[pv]:
            Q.appendleft(pv)
    else:
        if not dp[pv]:
            dp[pv] = dp[vn]
        else:
            dp[pv] += dp[vn]
        cld[pv] -= 1
        if not cld[pv]:
            Q.appendleft(pv)
print(len(leaf) - dp[1] + 1)

n = int(input())
a = [None] + list(map(int, input().split()))
p = [None, None] + list(map(int, input().split()))

g = [[] for i in range(n + 1)]
for i in range(n, 0, -1):
    if g[i]:
        c = 0
        x, y = 0, 10 ** 9
        for cv, wv in g[i]:
            c += cv
            x += wv
            y = min(y, wv)
        w = y if a[i] else x
    else:
        c, w = 1, 1
    if i == 1:
        print(c - w + 1)
    else:
        g[p[i]].append((c, w))

n = int(input())
a = [None] + list(map(int, input().split()))
p = [None, None] + list(map(int, input().split()))

f = [[0, 0, 10 ** 9] for i in range(n + 1)]
for i in range(n, 1, -1):
    if f[i][0] == 0:
        c = x = y = 1
    else:
        c, x, y = f[i]
    w = y if a[i] else x
    f[p[i]][0] += c
    f[p[i]][1] += w
    f[p[i]][2] = min(f[p[i]][2], w)
w = f[1][2] if a[1] else f[1][1]
print(f[1][0] - w + 1)

n = int(input())
is_max = list(map(int,input().split()))
parent = list(map(int,input().split()))
adjList = [[] for _ in range(n)]
used = [0]*n
for v, u in enumerate(parent):
    adjList[u-1].append(v+1)
for i in range(n-1, 0, -1):
    pi = parent[i-1] - 1
    if is_max[pi]:
        if used[pi] == 0:
            used[pi] = max(1, used[i])
        else:
            used[pi] = max(1, min(used[pi], used[i]))
    else:
        used[pi] += max(1, used[i])
k = sum([len(adjList[u]) == 0 for u in range(n)])
print(k-used[0]+1)


import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

n=int(input())
OP=[0]+list(map(int,input().split()))
f=list(map(int,input().split()))

CHLIST=[[] for i in range(n+1)]

for i in range(n-1):
    CHLIST[f[i]].append(i+2)

LEAFLIST=[]
for i in range(1,n+1):
    if len(CHLIST[i])==0:
        LEAFLIST.append(i)

LEAF=len(LEAFLIST)

HEIGHT=[0]*(n+1)
QUE=deque([1])
H=1
while QUE:
    NQUE=deque()
    while QUE:
        x=QUE.pop()
        HEIGHT[x]=H
        for ch in CHLIST[x]:
            NQUE.append(ch)

    H+=1
    QUE=NQUE

LIST=list(range(1,n+1))
LIST.sort(key=lambda x:HEIGHT[x],reverse=True)
        

NMINUS=[10]*(n+1)
def node_minus(n):
    if NMINUS[n]!=10:
        return NMINUS[n]
    
    if len(CHLIST[n])==0:
        return 0

    if OP[n]==1:
        ANS=-10**7

        for ch in CHLIST[n]:
            ANS=max(ANS,node_minus(ch))

        NMINUS[n]=ANS
        return ANS

    else:
        ANS=1

        for ch in CHLIST[n]:
            ANS-=1
            ANS+=node_minus(ch)

        NMINUS[n]=ANS
        return ANS
    

for l in LIST:
    node_minus(l)

print(LEAF+NMINUS[1])


import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

n=int(input())
OP=[0]+list(map(int,input().split()))
f=list(map(int,input().split()))

CHLIST=[[] for i in range(n+1)]

for i in range(n-1):
    CHLIST[f[i]].append(i+2)

LEAFLIST=[]
for i in range(1,n+1):
    if len(CHLIST[i])==0:
        LEAFLIST.append(i)

LEAF=len(LEAFLIST)

HEIGHT=[0]*(n+1)
QUE=deque([1])
H=1
while QUE:
    NQUE=deque()
    while QUE:
        x=QUE.pop()
        HEIGHT[x]=H
        for ch in CHLIST[x]:
            NQUE.append(ch)

    H+=1
    QUE=NQUE

LIST=list(range(1,n+1))
LIST.sort(key=lambda x:HEIGHT[x],reverse=True)
        

NMINUS=[10]*(n+1)
def node_minus(n):
    if NMINUS[n]!=10:
        return NMINUS[n]
    
    if len(CHLIST[n])==0:
        return 0

    if OP[n]==1:
        ANS=-10**7

        for ch in CHLIST[n]:
            ANS=max(ANS,node_minus(ch))

        NMINUS[n]=ANS
        return ANS

    else:
        ANS=1

        for ch in CHLIST[n]:
            ANS-=1
            ANS+=node_minus(ch)

        NMINUS[n]=ANS
        return ANS
    

for l in LIST:
    node_minus(l)

print(LEAF+NMINUS[1])

N = int(input())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
G = [[] for i in range(N)]
INF = float('inf')
for i in range(1, N):
    G[f[i - 1] - 1].append(i)

d = [0] * N
k = 0
for i in range(N - 1, -1, -1):
    if len(G[i]) == 0:
        d[i] = 1
        k += 1
        continue

    if a[i] == 0:
        d[i] = 0
        for j in G[i]:
            d[i] += d[j]
    else:
        d[i] = INF
        for j in G[i]:
            d[i] = min(d[i], d[j])

print(k - d[0] + 1)


N = int(input())
T = [int(a) for a in input().split()]
P = [int(a)-1 for a in input().split()]
C = [[] for i in range(N)]
for i in range(N-1):
    C[P[i]].append(i+1)
L = [i for i in range(N) if len(C[i]) == 0]
DP = [-1] * N
def calc(i):
    if len(C[i]) == 0:
        DP[i] = 1
    elif T[i]:
        mi = 10**10
        for j in C[i]:
            mi = min(mi, DP[j])
        DP[i] = mi
    else:
        s = 0
        for j in C[i]:
            s += DP[j]
        DP[i] = s

for i in range(N)[::-1]:
    calc(i)

print(len(L)-DP[0]+1)


N = int(input())
T = [int(a) for a in input().split()]
P = [int(a)-1 for a in input().split()]
C = [[] for i in range(N)]
for i in range(N-1):
    C[P[i]].append(i+1)
L = sum([1 for i in range(N) if len(C[i]) == 0])
DP = [-1] * N
def calc(i):
    if len(C[i]) == 0:
        DP[i] = 1
    else:
        X = [DP[j] for j in C[i]]
        DP[i] = min(X) if T[i] else sum(X)

for i in range(N)[::-1]:
    calc(i)

print(L-DP[0]+1)


N = int(input())
T = [int(a) for a in input().split()]
P = [-1] + [int(a)-1 for a in input().split()]
C = [[] for i in range(N)]
for i in range(1, N):
    C[P[i]].append(i)
L = sum([1 for i in range(N) if len(C[i]) == 0])
CT = [0] * N
DP = [-1] * N

i = 0
while i >= 0:
    # print("Try", i, C[i], CT[i])
    if CT[i] >= len(C[i]):
        if len(C[i]) == 0:
            DP[i] = 1
        elif T[i]:
            mi = 10**10
            for j in C[i]:
                mi = min(mi, DP[j])
            DP[i] = mi
        else:
            s = 0
            for j in C[i]:
                s += DP[j]
            DP[i] = s
        i = P[i]
    else:
        CT[i], i = CT[i] + 1, C[i][CT[i]]

print(L-DP[0]+1)


import sys,threading
sys.setrecursionlimit(10**6)
threading.stack_size(10**8)

def main():
    n = int(input().strip())
    son_tree = dict()
    min_max = [0] + [int(i) for i in input().strip().split()]
    for child, parent in enumerate(map(int,input().strip().split()),2):
        if parent not in son_tree:
            son_tree[parent] = [child]
        else:
            son_tree[parent].append(child)
    leaf_ctr = 0
    dp = [0 for i in range(n+1)]
    for i in range(1,n+1):
        if i not in son_tree:
            leaf_ctr += 1
            dp[i] = 1

    visited = set()
    def dfs(node):
        if node not in visited:
            visited.add(node)
            if node not in son_tree:
                return 1
            if min_max[node]:
                for i in son_tree[node]:
                    dfs(i)
                dp[node] = min(dp[k] for k in son_tree[node])
                return 0
            else:
                for i in son_tree[node]:
                    dfs(i)
                dp[node] = sum(dp[k] for k in son_tree[node])
                return 0
    dfs(1)
    print(leaf_ctr - dp[1]+1)
def __starting_point():
    t = threading.Thread(target=main)
    t.start()
    t.join()

__starting_point()

n=int(input())
oper=list(map(int,input().split()))
rod=list(map(int,input().split()))
s={i:[] for i in range(n)}
for i in range(len(rod)):
    s[rod[i]-1].append(i+1)
t=[-1]*n
t[0]=1
black=[]
grey=[0]
while grey!=[]:
    black.append(grey.pop())
    for i in s[black[-1]]:
        grey.append(i)
        t[i]=t[black[-1]]+1
l=[[] for i in range(n+1)]
for i in range(len(t)):
    l[t[i]].append(i)
l.reverse()
t=[i for i in s if s[i]==[]]
ans=[1 for i in range(n)]
for i in t:
    ans[i]=-1
for x in l:
    for y in x:
        if oper[y]==0:
            if ans[y]!=1:
                continue
            ans[y]=sum([ans[i] for i in s[y]])
        elif oper[y]==1:
            if ans[y]!=1:
                continue
            ans[y]=max([ans[i] for i in s[y]])
t=len([i for i in s if s[i]==[]])
print(t+1+ans[0])

    



import math
import sys
input = sys.stdin.readline


def main():

    n = int(input())
    min_max = list(map(int, input().split()))
    tree_input = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for v in range(n-1):
        parent = tree_input[v]-1
        tree[parent].append(v+1)

    leaves = [0 for _ in range(n)]

    stack = [0]
    pos = 0
    while pos < len(stack):
        for child in tree[stack[pos]]:
            stack.append(child)
        pos += 1

    for pos in range(len(stack)-1, -1, -1):
        node = stack[pos]
        if len(tree[node]) == 0:
            leaves[node] = 1
        else:
            for child in tree[node]:
                leaves[node] += leaves[child]

    node_max = [0] * n
    for pos in range(len(stack)-1, -1, -1):
        node = stack[pos]
        if len(tree[node]) == 0:
            node_max[node] = 1
        else:
            if min_max[node] == 1:
                best = math.inf
                for child in tree[node]:
                    if leaves[child] - node_max[child] < best:
                        best = leaves[child] - node_max[child]
                node_max[node] = leaves[node] - best
            else:
                optimal = 1
                for child in tree[node]:
                    optimal += node_max[child]-1
                node_max[node] = optimal

    print(node_max[0])


def __starting_point():
    main()

__starting_point()

n=int(input())
op=[int(g) for g in input().split()]
parent=[int(g)-1 for g in input().split()]
tree=[[] for i in range(n)]
for i in range(n-1):
    tree[parent[i]].append(i+1)
l=sum([1 for i in range(n) if len(tree[i])==0])

l1=[-1]*n
for i in range(n-1,-1,-1):
    if len(tree[i])==0:
        l1[i]=1
    else:
        x=[l1[j] for j in tree[i]]
        if op[i]==1:
            l1[i]=min(x)
        else:
            l1[i]=sum(x)
print(l-l1[0]+1)

        
    
    

    
                
    




n = int(input())
oper = list(map(int, input().split())) # 0 - min, 1 - max
prev = [int(i) - 1 for i in input().split()]

graph = [[] for i in range(n)]
for i in range(n - 1):
    graph[prev[i]].append(i + 1) 

leafs = sum([len(graph[i]) == 0 for i in range(n)])

res = [0] * n
for u in range(n - 1, -1, -1):
    if len(graph[u]) == 0:
        res[u] = 1
    else:
        sons = [res[v] for v in graph[u]]
        res[u] = (min(sons) if oper[u] == 1 else sum(sons))
print(leafs - res[0] + 1)

import sys

n = int(input())
minmax = list(map(int, input().split()))
childs = [[] for i in range(n)]

for idx, father in enumerate(list(map(int,input().split()))):
    childs[father-1].append(idx+1)

stack = []
stack.append(0)

ans = [None for ele in range(n)]
vis = [False for ele in range(n)]

while len(stack):
    index = stack[-1]
    if not vis[index]:
        vis[index] = True
        if len(childs[index]) == 0:
            ans[index] = (0,1)
            stack.pop()
        else:
            for child in childs[index]:
                stack.append(child)
    else:
        stack.pop()
        res = [ans[child] for child in childs[index]]
        total = sum([ele[1] for ele in res])
        if minmax[index] == 1:
            ans[index] = min([ele[0] for ele in res]), total
        else:
            bigger_than = sum([ele[1] - ele[0] - 1 for ele in res])    
            ans[index] = total - bigger_than - 1, total

print(ans[0][1] - ans[0][0])


n = int(input())
op = list(int(v) for v in input().split())
f = list(int(v) for v in input().split())

ch = [list() for p in op]

for i, p in enumerate(f): ch[p-1].append(i+1) # children

q = [0]
nl = 0 # number of leaves

for i in range(n): q += ch[i] # parents first

x = [0]*n

while q:
    p = q.pop()
    if not ch[p]:
        #print('node', p, 'is a leaf (0)')
        nl += 1
    elif op[p]:
        x[p] = min(x[c] for c in ch[p])
        #print('node', p, 'is a MAX (%d)' % x[p], ch[p], list(x[c] for c in ch[p]))
    else:
        x[p] = sum(x[c]+1 for c in ch[p])-1
        #print('node', p, 'is a MIN', x[p])

print(nl - x[0])
#print(n, op, f)
#print(ch)
#print(q)


n = int(input())
op = list(int(v) for v in input().split())
f = list(int(v) for v in input().split())

ch = [list() for p in op]
for i, p in enumerate(f): ch[p-1].append(i+1) # children

q = [0]
for i in range(n): q += ch[q[i]] # queue parents first

x = [0]*n # how many largest numbers the node consumes
nl = 0 # number of leaves

for p in reversed(q):
    if not ch[p]: nl += 1
    elif op[p]: x[p] = min(x[c] for c in ch[p])
    else: x[p] = sum(x[c]+1 for c in ch[p])-1

print(nl - x[0])


n = int(input())
op = [int(v) for v in input().split()]
f = [int(v) for v in input().split()]

ch = [[] for p in op]
for i, p in enumerate(f): ch[p-1].append(i+1) # children

q = [0]
for i in range(n): q += ch[q[i]] # queue parents first

x = [0]*n # how many largest numbers the node consumes
nl = 0 # number of leaves

for p in q[::-1]: # Leaves first
    if not ch[p]: nl += 1
    elif op[p]: x[p] = min(x[c] for c in ch[p])
    else: x[p] = sum(x[c]+1 for c in ch[p])-1

print(nl - x[0])