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]

Source: codeforces | Difficulty: HARD
Tags: Graph traversal

Sorting

Greedy algorithms

Tree algorithms

Dynamic programming


Solutions:
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())
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)


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


from operator import attrgetter
n = int(input().strip())

class Kg:
	lists = 0
	order = []

	def __init__(self, i, mm):
		self.index = i
		self.minmax = mm
		self.next = []
		self.todo = []
		self.vol = 0

	def add(self, item):
		self.next.append(item)

	def count(self):
		if self.next:
			if self.minmax:
				self.vol = min((item.vol for item in self.next))
			else:
				self.vol = sum((item.vol for item in self.next))
		else:
			self.vol = 1
			type(self).lists += 1

	def hang(self):
		queue = [self]
		while queue:
			cur = queue.pop()
			type(self).order.append(cur)
			queue.extend(cur.next)
total = [Kg(i, mm) for (i, mm) in enumerate(map(int, input().split()))]
for (i, v) in enumerate(map(int, input().split())):
	total[v - 1].add(total[i + 1])
root = total[0]
root.hang()
for item in reversed(Kg.order):
	item.count()
print(Kg.lists - root.vol + 1)


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)
q = [0]
for i in range(n):
	q += ch[q[i]]
x = [0] * n
nl = 0
for p in q[::-1]:
	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())
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 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)


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


import sys
input = sys.stdin.readline

def dfs(u):
	stack = [u]
	visit = [False] * n
	while stack:
		u = stack[-1]
		if not visit[u]:
			visit[u] = True
			for v in child[u]:
				stack.append(v)
		else:
			if len(child[u]) == 0:
				global k
				k += 1
			elif a[u]:
				res[u] = 10 ** 10
				for v in child[u]:
					res[u] = min(res[u], res[v])
			else:
				res[u] = len(child[u]) - 1
				for v in child[u]:
					res[u] += res[v]
			stack.pop()
n = int(input())
a = list(map(int, input().split()))
par = list(map(int, input().split()))
child = [[] for _ in range(n)]
for i in range(n - 1):
	c = i + 1
	par[i] -= 1
	p = par[i]
	child[p].append(c)
res = [0] * n
k = 0
dfs(0)
ans = k - res[0]
print(ans)


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
		elif 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())
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)


R = lambda : map(int, input().split())
n = int(input())
fcs = [0] + list(R())
ps = [0, 0] + list(R())
cs = [1] * (n + 1)
for i in range(2, n + 1):
	cs[ps[i]] = 0
nc = sum(cs) - 1
for i in range(n, 1, -1):
	if fcs[ps[i]] == 0:
		cs[ps[i]] += cs[i]
	elif not cs[ps[i]]:
		cs[ps[i]] = cs[i]
	else:
		cs[ps[i]] = min(cs[ps[i]], cs[i])
print(nc - cs[1] + 1)


import sys
input = sys.stdin.readline

def dfs(u):
	stack = [u]
	visit = [False] * n
	while stack:
		u = stack[-1]
		if not visit[u]:
			visit[u] = True
			for v in child[u]:
				stack.append(v)
		else:
			if len(child[u]) == 0:
				global k
				k += 1
			elif a[u]:
				res[u] = 10 ** 10
				for v in child[u]:
					res[u] = min(res[u], res[v])
			else:
				res[u] = len(child[u]) - 1
				for v in child[u]:
					res[u] += res[v]
			stack.pop()

def dfs2():
	next = [0] * n
	i = 0
	while i >= 0:
		if next[i] < len(child[i]):
			(next[i], i) = (next[i] + 1, child[i][next[i]])
		else:
			if len(child[i]) == 0:
				pass
			elif a[i]:
				dp[i] = 10 ** 10
				for j in child[i]:
					dp[i] = min(dp[i], dp[j])
			else:
				dp[i] = len(child[i]) - 1
				for j in child[i]:
					dp[i] += dp[j]
			i = par[i]
n = int(input())
a = [int(i) for i in input().split()]
par = [-1] + [int(i) - 1 for i in input().split()]
child = [[] for _ in range(n)]
for i in range(1, n):
	child[par[i]].append(i)
k = sum((1 for i in range(n) if len(child[i]) == 0))
dp = [0] * n
dfs2()
ans = k - dp[0]
print(ans)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, OrderedDict
import math
import heapq

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
minmax = [-1] + listIn()
f = listIn()
nonleaf = set(f)
f = [-1] * 2 + f
root_tree = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	root_tree[f[i]].append(i)
leaf_nodes = set(range(1, n + 1)) - nonleaf
count_leaf = len(leaf_nodes)
arr = [-1] * (n + 1)
child_count = [0] * (n + 1)
for i in range(1, n + 1):
	child_count[i] = len(root_tree[i])
for i in range(n, 0, -1):
	if child_count[i] == 0:
		arr[i] = 1
	else:
		temp = []
		for child in root_tree[i]:
			temp.append(arr[child])
		if minmax[i] == 1:
			arr[i] = min(temp)
		else:
			arr[i] = sum(temp)
print(count_leaf - arr[1] + 1)


from collections import defaultdict
from collections import deque
n = int(input())
tree = defaultdict(set)
node_type = list(map(int, input().split()))
values = [0 for x in range(n)]
for (i, x) in enumerate(map(int, input().split())):
	tree[x - 1].add(i + 1)
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]))
k = n - len(tree)
print(k - values[0] + 1)


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())
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())
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(leaves - d[0] + 1)


from collections import defaultdict
from math import log2
from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10 ** 5)

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

@bootstrap
def dfs(u):
	m = -float('inf')
	s = []
	l = 0
	for j in adj[u]:
		yield dfs(j)
		size[u] += size[j]
		m = max(m, val[j])
		s.append(val[j])
		l += 1
	s.sort()
	curr = size[u]
	m1 = curr
	for k in s:
		m1 = min(m1, curr + k)
		curr += k - 1
	if a[u]:
		val[u] = m
	else:
		val[u] = m1 - size[u]
	if adj[u] == []:
		size[u] = 1
		val[u] = 0
	yield
	yield
n = int(input())
adj = [[] for i in range(n + 1)]
a = [0] + list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n - 1):
	adj[b[i]].append(i + 2)
size = [0 for i in range(n + 1)]
val = [-float('inf') for i in range(n + 1)]
dfs(1)
print(size[1] + val[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:
	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 io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
INF = 10 ** 9
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, INF)
		for (cv, wv) in g[i]:
			c += cv
			x += cv - wv + 1
			y = min(y, cv - wv)
		w = c - y if a[i] else c - x + 1
	else:
		c = w = 1
	if i == 1:
		print(w)
	else:
		g[p[i]].append((c, w))


import sys
import math
import collections
from pprint import pprint as pp
mod = 998244353
MAX = 10 ** 15

def inp():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def vector(size, val=0):
	vec = [val for i in range(size)]
	return vec

def matrix(rowNum, colNum, val=0):
	mat = []
	for i in range(rowNum):
		collumn = [val for j in range(colNum)]
		mat.append(collumn)
	return mat
n = int(input())
val = [0] + array()
par = [0, 0] + array()
dp = vector(n + 5)
leaf = 0
adj = matrix(n + 5, 0)
for i in range(2, n + 1):
	adj[par[i]].append(i)
for i in range(n, 0, -1):
	if len(adj[i]) == 0:
		dp[i] = 1
		leaf += 1
	elif val[i] == 1:
		dp[i] = MAX
		for x in adj[i]:
			dp[i] = min(dp[i], dp[x])
	else:
		for x in adj[i]:
			dp[i] += dp[x]
print(leaf + 1 - dp[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())
oper = list(map(int, input().split()))
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)


n = int(input())
least = [1] * (n + 1)
op = [0] + list(map(int, input().split()))
pa = [0, 0] + list(map(int, input().split()))
for i in range(1, n + 1):
	least[pa[i]] = 0
leaves = sum(least)
for i in range(n, 1, -1):
	if op[pa[i]] == 0:
		least[pa[i]] += least[i]
	elif least[pa[i]] == 0:
		least[pa[i]] = least[i]
	else:
		least[pa[i]] = min(least[pa[i]], least[i])
print(leaves - least[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))


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())
dp = [1] * n
o = list(map(int, input().split()))
p = list(map(int, input().split()))
for i in range(n - 1):
	p[i] -= 1
for i in p:
	dp[i] = 0
count = sum(dp)
for i in range(n - 2, -1, -1):
	if not o[p[i]]:
		dp[p[i]] += dp[i + 1]
	elif not dp[p[i]]:
		dp[p[i]] = dp[i + 1]
	else:
		dp[p[i]] = min(dp[i + 1], dp[p[i]])
print(count - dp[0] + 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)