Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.

Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.

The definition of a rooted tree can be found here.


-----Input-----

The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer p_{i} (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ p_{i} ≤ i).

Vertex 1 is the root. It's guaranteed that the root has at least 2 children.


-----Output-----

Print "Yes" if the tree is a spruce and "No" otherwise.


-----Examples-----
Input
4
1
1
1

Output
Yes

Input
7
1
1
1
2
2
2

Output
No

Input
8
1
1
1
1
3
3
3

Output
Yes



-----Note-----

The first example:

[Image]

The second example:

$8$

It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.

The third example:

[Image]

Difficulty: EASY
Tags: Tree algorithms

Implementation
Expected Time Complexity: O(N)

Starter code:

Solutions:
import sys
readline = sys.stdin.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nn(n):
	return [int(readline()) for i in range(n)]

def nnp(n, x):
	return [int(readline()) + x for i in range(n)]

def nmp(n, x):
	return (int(readline()) + x for i in range(n))

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sl():
	return [s for s in readline().split()]

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
n = i1()
arr = nn(n - 1)
cnt = [0] * (n + 1)
leaf = [True] * (n + 1)
i = n
arr.reverse()
ok = True
for p in arr:
	if leaf[i]:
		cnt[p] += 1
	elif cnt[i] < 3:
		ok = False
		break
	leaf[p] = False
	i -= 1
print('YES' if ok and cnt[1] >= 3 else 'NO')


n = int(input())
adj = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
	p = int(input())
	adj[p].append(i)
for i in range(1, n + 1):
	if len(adj[i]) == 0:
		continue
	leaf = 0
	for j in adj[i]:
		if len(adj[j]) == 0:
			leaf += 1
	if leaf < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
a = [[] for i in range(n)]
barn = [False] * n
for i in range(n - 1):
	q = int(input())
	a[q - 1].append(i + 1)
	barn[q - 1] = True
for z in range(n):
	ja = True
	if len(a[z]) > 0:
		if len(a[z]) > 2:
			raknare = 0
			for f in range(len(a[z])):
				if barn[a[z][f]] == False:
					raknare += 1
			if raknare < 3:
				print('No')
				ja = False
				break
		else:
			print('No')
			ja = False
			break
if ja == True:
	print('Yes')


n = int(input())
a = [0] * (n + 1)
d = [0] * (n + 1)
b = [0] * (n + 1)
for i in range(2, n + 1):
	c = int(input())
	a[c] += 1
	b[i] = c
for i in range(2, n + 1):
	if a[i] == 0:
		d[b[i]] += 1
for i in range(1, n + 1):
	if d[i] < 3 and a[i] != 0:
		exit(print('No'))
print('Yes')


n = int(input())
tree = [[0] for i in range(n + 1)]
for i in range(n - 1):
	p = int(input())
	tree[p][0] += 1
	tree[p].append(i + 2)
flag = True
for i in range(1, n + 1):
	if tree[i][0] > 0:
		if len(tree[i]) < 4:
			flag = False
			break
		count = 0
		for j in tree[i][1:]:
			if tree[j][0] == 0 or len(tree[j]) == 1:
				count += 1
		if count < 3:
			flag = False
			break
if flag:
	print('Yes')
else:
	print('No')


n = int(input())
gr = [[] for i in range(n)]
for i in range(n - 1):
	p = int(input())
	gr[p - 1].append(i + 1)
leaf = [0] * n
for i in range(n):
	if len(gr[i]) == 0:
		leaf[i] = 1
ans = 1
for i in range(n):
	if leaf[i] == 0:
		s = 0
		for q in gr[i]:
			s += leaf[q]
		if s < 3:
			ans = 0
if ans == 1:
	print('Yes')
else:
	print('No')


n = int(input())
adj = []
MAX_N = 1005
for i in range(MAX_N):
	adj.append([])
for i in range(2, n + 1, 1):
	adj[int(input())].append(i)

def dfs(u):
	cnt = 0
	for v in adj[u]:
		if len(adj[v]) == 0:
			cnt += 1
		elif not dfs(v):
			return False
	return cnt > 2
print('Yes' if dfs(1) else 'No')


n = int(input())
li = [int(input()) for i in range(n - 1)]
counts = [0] * n
for i in range(n - 1):
	q = li[i] - 1
	counts[q] += 1
oldc = counts.copy()
for i in range(1, n):
	if counts[i] > 0:
		counts[li[i - 1] - 1] -= 1
for i in range(n):
	if counts[i] < 3 and (counts[i] == 0 and oldc[i] != 0 or counts[i] != 0):
		print('No')
		exit(0)
print('Yes')


n = int(input())
child = {}
for i in range(2, n + 1):
	f = int(input())
	if f not in child:
		child[f] = set()
	child[f].add(i)
for i in range(1, n + 1):
	if i in child:
		t = 0
		for x in child[i]:
			if x not in child:
				t += 1
		if t < 3:
			print('No')
			exit(0)
print('Yes')


def dfs(x):
	cnt = 0
	global flag
	for v in child[x]:
		if len(child[v]):
			dfs(v)
		else:
			cnt += 1
	if cnt < 3:
		flag = False
n = int(input())
flag = True
child = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	a = int(input())
	child[a].append(i)
dfs(1)
print(('NO', 'YES')[flag])


class TreNode:

	def __init__(self, parent):
		self.children = []
		self.parent = parent

	def isLeaf(self):
		return self.children == []
AllNodes = {}
AllNodes[1] = TreNode(-1)
a = int(input())
if a < 2:
	print('No')
	quit()
for i in range(2, a + 1):
	ThisParent = int(input())
	if i in list(AllNodes.keys()):
		AllNodes[i].parent = ThisParent
	else:
		AllNodes[i] = TreNode(ThisParent)
	if ThisParent not in list(AllNodes.keys()):
		AllNodes[ThisParent] = TreNode(-1)
	AllNodes[ThisParent].children.append(i)
for node in list(AllNodes.keys()):
	thisnodeleafcount = 0
	for child in AllNodes[node].children:
		if len(AllNodes[child].children) == 0:
			thisnodeleafcount += 1
	if thisnodeleafcount < 3 and (not AllNodes[node].isLeaf()):
		print('No')
		quit()
print('Yes')


n = int(input())
child = {}
for i in range(n - 1):
	l = int(input())
	if l in child.keys():
		child[l].append(i + 2)
	else:
		child[l] = []
		child[l].append(i + 2)
for i in child.keys():
	count = 0
	flag = 0
	for j in child[i]:
		if j not in child.keys():
			count += 1
	if count < 3:
		flag = 1
		break
if flag == 1:
	print('No')
else:
	print('Yes')


import logging
import sys
from collections import namedtuple
from sys import stdin
nivel_log = logging.ERROR
logger_cagada = None

class nodot:

	def __init__(self, idx):
		self.idx = idx
		self.es_hoja = False
		self.hijos = []

	def __repr__(self):
		return '{}:{}:{}'.format(self.idx, self.es_hoja, list(map(lambda x: x.idx, self.hijos)))

def chriscaca_sputa_es_valido(nodo):
	hijos_hoja_cnt = 0
	if not nodo:
		return True
	hijos_hoja_cnt = len(list(filter(lambda nd: nd.es_hoja, nodo.hijos)))
	assert nodo.es_hoja or len(nodo.hijos)
	return nodo.es_hoja or hijos_hoja_cnt > 2

def chriscaca_sputa_es_hoja(nodo):
	if nodo:
		nodo.es_hoja = not len(nodo.hijos)
		logger_cagada.debug('q pedo {}'.format(nodo))

def chriscaca_sputa_core(lista_adjacencia):
	list(map(chriscaca_sputa_es_hoja, lista_adjacencia))
	logger_cagada.debug('la lista de mierda  a {}'.format(lista_adjacencia))
	return all(map(lambda nodo: chriscaca_sputa_es_valido(nodo), lista_adjacencia))

def caca_comun_lee_linea_como_num():
	return int(stdin.readline().strip())

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

def chriscaca_sputa_main():
	nodo_cnt = caca_comun_lee_linea_como_num()
	lista_caca = [None]
	for nodo_idx in range(1, nodo_cnt + 1):
		nodon = nodot(nodo_idx)
		lista_caca.append(nodon)
	for nodo_idx in range(2, nodo_cnt + 1):
		padre_idx = caca_comun_lee_linea_como_num()
		padre = lista_caca[padre_idx]
		padre.hijos.append(lista_caca[nodo_idx])
	logger_cagada.debug('la lista de mierda {}'.format(lista_caca))
	fuck = chriscaca_sputa_core(lista_caca)
	logger_cagada.debug('puta {}'.format(fuck))
	if fuck:
		print('Yes')
	else:
		print('No')
FORMAT = '[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s'
logging.basicConfig(level=nivel_log, format=FORMAT)
logger_cagada = logging.getLogger('asa')
logger_cagada.setLevel(nivel_log)
chriscaca_sputa_main()


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	l = []
	for i in range(2, n + 1):
		if prime[i]:
			l.append(i)
	return l

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, ll, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		if val % ll[mid] == 0:
			ans = ll[mid]
			return binary(l, mid - 1, ll, val, ans)
		else:
			return binary(l, mid - 1, ll, val, ans)

def odd(n, ans, s):
	if n % 2 != 0 or n in s:
		return ans
	else:
		s.add(n)
		return odd(n // 2, ans + 1, s)
n = int(input())
p = [int(input()) - 1 for _ in range(n - 1)]
leafs = list(filter(lambda x: not x in p, range(n)))
lp = [x for (i, x) in enumerate(p) if i + 1 in leafs]
x = min((lp.count(k) for k in p))
print('Yes' if x >= 3 else 'No')


n = list(map(int, input().strip().split()))[0]
root = dict()
leaf = dict()
leaf[1] = 0
for i in range(2, n + 1):
	connect = list(map(int, input().strip().split()))[0]
	if connect in root:
		root[connect].append(i)
		leaf[i] = 0
	else:
		root[connect] = []
		root[connect].append(i)
		leaf[i] = 0
		leaf.pop(connect)
answer = 'Yes'
for parents in root.keys():
	count = 0
	for child in root[parents]:
		if child in leaf:
			count += 1
	if count < 3:
		answer = 'No'
		break
print(answer)


n = int(input())
p = [int(input()) for i in range(n - 1)]
ver = [i for i in range(1, n + 1) if i in p]
lis = [j for (i, j) in enumerate(p, start=2) if i not in ver]
b = []
for i in ver:
	if i in lis:
		b.append(lis.count(i))
	else:
		b.append(0)
print('Yes' if min(b) >= 3 else 'No')
b.clear()


n = int(input())
G = [[0] for i in range(n + 1)]
ans = 'Yes'
for i in range(1, n):
	x = int(input())
	G[x][0] += 1
	G[x].append(i + 1)
for i in range(1, n):
	if G[i][0] > 0:
		for j in range(1, len(G[i])):
			if G[G[i][j]][0] > 0:
				G[i][0] -= 1
		if G[i][0] < 3:
			ans = 'No'
print(ans)


def main():
	n = int(input())
	g = [[] for _ in range(n)]
	for v in range(1, n):
		g[int(input()) - 1].append(v)
	for vv in g:
		if vv and sum((not g[v] for v in vv)) < 3:
			print('No')
			return
	print('Yes')
main()


n = int(input())
(l, a) = (2, {})
for i in range(n - 1):
	t = int(input())
	if t in a:
		a[t].append(l)
	else:
		a[t] = [l]
	l += 1
p = 0
for i in a:
	k = 0
	for j in a[i]:
		if j in a:
			continue
		else:
			k += 1
	if k < 3:
		p = p + 1
		break
if p == 1:
	print('No')
else:
	print('Yes')


n = int(input())
(tree, bol, ckd) = ([[] for i in range(n)], True, [])
for i in range(n - 1):
	j = int(input())
	tree[j - 1] += [i + 1]
for k in range(len(tree)):
	if k not in ckd:
		bol1 = 0
		for i in tree[k]:
			bol1 += int(tree[i] == [])
			if tree[i] == []:
				ckd += [i]
		if bol1 < 3:
			bol = False
			break
if bol:
	print('Yes')
else:
	print('No')


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin, tan

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

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
n = ii()
a = [[] for _ in range(n + 3)]
for i in range(2, n + 1):
	x = ii()
	a[x].append(i)
f = 0
for i in a:
	c = 0
	for v in i:
		if not a[v]:
			c += 1
	if i and c < 3:
		f = 1
		break
print(['Yes', 'No'][f])


n = int(input())
tree = dict()
tree[0] = [0, True, 0]
is_pruce = 'Yes'
for i in range(1, n):
	tree[i] = [int(input()), True, 0]
	parent = tree.get(i)[0] - 1
	if tree[parent][1] is True:
		tree[parent][1] = False
for i in range(1, n):
	parent = tree.get(i)[0] - 1
	if tree[i][1] is True:
		tree[parent][2] += 1
for values in tree.values():
	if values[1] is False and values[2] <= 2:
		is_pruce = 'No'
		break
print(is_pruce)


n = int(input())
m = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	m[int(input())].append(i)
for i in m:
	if not i:
		continue
	k = 0
	for j in i:
		if not m[j]:
			k += 1
		if k == 3:
			break
	else:
		print('NO')
		break
else:
	print('YES')


n = int(input())
a = [0] * n
c = [0] * (n + 5)
li = []
for i in range(1, n):
	a[i] = int(input()) - 1
	c[a[i]] += 1
p = [0] * n
for i in range(n):
	if c[i] == 0:
		li.append(i)
		p[a[i]] += 1
cli = len(li)
for i in range(n):
	if p[i] >= 3:
		cli += 1
print('Yes' if cli == n else 'No')


class Node:

	def __init__(self, value):
		self.data = value
		self.children = []

	def add_children(self, value):
		self.children.append(Node(value))

class Tree:

	def __init__(self):
		self.root = Node(1)

	def Search(self, value, node=None):
		if node is None:
			node = self.root
		q = None
		if node.data == value:
			return node
		else:
			for i in range(len(node.children)):
				q = self.Search(value, node.children[i])
				if q is not None:
					return q
		return q

	def Insert(self, value, parent):
		node = self.Search(parent)
		node.add_children(value)

	def is_spruce(self, node=None):
		if node == None:
			node = self.root
		for i in range(len(node.children)):
			spruce = self.is_spruce(node.children[i])
			if not spruce:
				return spruce
		if len(node.children) > 0:
			num = 0
			for i in range(len(node.children)):
				child = node.children[i]
				if len(child.children) == 0:
					num = num + 1
			if num < 3:
				return False
		return True
n = int(input())
tree = Tree()
for i in range(n - 1):
	parent = int(input())
	value = i + 2
	tree.Insert(value, parent)
if tree.is_spruce():
	print('Yes')
else:
	print('No')


n = int(input())
isLeaf = [True] * n
leafCount = [0] * n
par = [0] * n
for i in range(1, n):
	par[i] = int(input()) - 1
	isLeaf[par[i]] = False
for i in range(1, n):
	if isLeaf[i]:
		leafCount[par[i]] += 1
for i in range(n):
	if not isLeaf[i] and leafCount[i] < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
children = [0] * n
parents = []
for i in range(n - 1):
	x = int(input())
	x -= 1
	children[x] += 1
	parents.append(x)
result = [0] * n
temp = 0
for i in range(n):
	if children[i] == 0:
		temp += 1
		result[parents[i - 1]] += 1
if result.count(0) + result.count(1) + result.count(2) == temp:
	print('Yes')
else:
	print('No')


def eto_yolka(babies):
	for i in range(len(babies)):
		answer = 0
		for j in range(len(babies[i])):
			if eto_list(babies, babies[i][j]):
				answer = answer + 1
		if babies[i] != [] and answer < 3:
			return False
	return True

def eto_list(babies, i):
	if babies[i] != []:
		return 0
	else:
		return 1
n = int(input())
babies = []
for i in range(n):
	babies.append([])
for i in range(n - 1):
	p = int(input())
	babies[p - 1].append(i + 1)
if eto_yolka(babies):
	print('Yes')
else:
	print('No')


n = int(input())
G = {v: set() for v in range(1, n + 1)}
for v in range(2, n + 1):
	G[int(input())].add(v)
for v in G:
	if G[v] and len([u for u in G[v] if not G[u]]) < 3:
		print('No')
		break
else:
	print('Yes')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [tuple(map(int, l.split())) for l in sys.stdin]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = [I() for _ in range(n - 1)]
	s = set(a)
	c = collections.Counter([a[i] for i in range(n - 1) if i + 2 not in s])
	if all((c[i] > 2 for i in s)):
		return 'Yes'
	return 'No'
print(main())


n = int(input())
l = [0]
for i in range(n - 1):
	x = int(input())
	l.append(x)
a = []
for i in range(1, n):
	if l[i] not in a:
		a.append(l[i])
for i in range(len(a)):
	l[a[i] - 1] = 0
l.sort()
freq = {}
v = []
for items in l:
	freq[items] = l.count(items)
for (key, value) in freq.items():
	v.append([key, value])
x = 0
for i in range(len(v)):
	if v[i][0] > 0:
		if v[i][1] < 3 or len(a) != len(v) - 1:
			print('NO')
			x = 1
			break
if x == 0:
	print('YES')


n = int(input())
nonleaf = [0 for i in range(1010)]
child = [[] for i in range(1010)]
leaf = [0 for i in range(1010)]

def dfs(s):
	cnt = 0
	for chd in child[s]:
		cnt += dfs(chd)
	leaf[s] = cnt
	return 1 - nonleaf[s]
for i in range(2, n + 1):
	node = int(input())
	child[node].append(i)
	nonleaf[node] = 1
dfs(1)
for i in range(1, n + 1):
	if nonleaf[i] and leaf[i] < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
dic = {}
for i in range(2, n + 1):
	x = int(input())
	if x not in dic.keys():
		dic.update({x: []})
		dic[x].append(i)
	else:
		dic[x].append(i)
for x in dic.keys():
	c = []
	for y in dic[x]:
		if y not in dic.keys():
			c.append(y)
	dic[x] = c
st = True
for y in dic.values():
	if len(y) < 3:
		st = False
		break
if st:
	print('Yes')
else:
	print('No')


read = lambda : list(map(int, input().split()))
n = int(input())
g = [list() for i in range(n + 1)]
for i in range(n - 1):
	p = int(input())
	g[p].append(i + 2)
c = [0] * (n + 1)
for i in range(1, n + 1):
	if len(g[i]) == 0:
		c[i] = 1
for i in range(1, n + 1):
	if not c[i]:
		cnt = 0
		for u in g[i]:
			cnt += c[u]
		if cnt < 3:
			print('No')
			return
print('Yes')


n = int(input())
adjList = []
for i in range(0, n):
	adjList.append([])
for i in range(0, n - 1):
	inputLine = int(input())
	adjList[inputLine - 1].append(i + 1)
	adjList[i + 1].append(inputLine - 1)
subTrees = []
for i in range(0, n):
	subTrees.append(-1)
visited = []
for i in range(0, n):
	visited.append(False)
roots = []
for i in range(0, n):
	roots.append(False)

def dfs(x):
	totalSize = 0
	visited[x] = True
	isLeaf = True
	if len(adjList[x]) > 1:
		roots[x] = True
	for i in range(0, len(adjList[x])):
		if visited[adjList[x][i]]:
			continue
		child = adjList[x][i]
		dfs(child)
		if roots[child] == True:
			totalSize -= 1
		totalSize += 1
	if roots[x] == False:
		subTrees[x] = -100000
	else:
		subTrees[x] = totalSize
dfs(0)
isSpruce = True
if n == 3:
	print('No')
else:
	for i in range(0, n):
		if subTrees[i] < 3 and subTrees[i] != -100000:
			print('No')
			isSpruce = False
			break
	if isSpruce:
		print('Yes')


n = int(input())
a = {}
for i in range(n - 1):
	s = int(input())
	if s in a:
		a[s].append(i + 2)
	else:
		a[s] = [i + 2]
for i in a:
	q = 0
	for j in a[i]:
		if not j in a:
			q += 1
	if q < 3:
		print('No')
		exit()
print('Yes')


class Node:

	def __init__(self, num):
		self.num = num
		self.children = []

	def add_child(self, child):
		self.children.append(child)

	def is_leaf(self):
		return len(self.children) == 0

	def check_spruciness(self):
		if self.is_leaf():
			return True
		leafs = 0
		for child in self.children:
			if child.is_leaf():
				leafs += 1
			elif not child.check_spruciness():
				return False
		return leafs >= 3

	def description(self):
		string = str(self.num) + '{'
		for child in self.children:
			string += child.description()
		return string + '}'

class Tree:

	def __init__(self, count, root):
		self.count = count
		self.root = root

	@classmethod
	def get_tree_from_array(cls, tree):
		assert len(tree) == tree[0]
		count = tree[0]
		tree = tree[1:]
		nodes = [Node(1)]
		for i in range(len(tree)):
			nodes.append(Node(i + 1))
		for i in range(len(tree)):
			parentNum = tree[i] - 1
			if parentNum >= 0 and parentNum < len(nodes):
				nodes[parentNum].add_child(nodes[i + 1])
			else:
				return None
		root = nodes[0]
		return cls(count=count, root=root)

	def check_spruce(self):
		if self.root.check_spruciness():
			print('Yes')
		else:
			print('No')
count = int(input())
list = [count]
for i in range(count - 1):
	x = int(input())
	list.append(x)
tree = Tree.get_tree_from_array(list)
tree.check_spruce()


n = int(input())
a = [0 for i in range(n)]
a[0] = -1
for i in range(1, n):
	a[i] = int(input()) - 1
b = [0 for i in range(n)]
for i in range(1, len(a)):
	if i not in a:
		b[i] = -1
		b[a[i]] += 1
got = True
for i in b:
	if i != -1 and i < 3:
		got = False
		break
if got:
	print('Yes')
else:
	print('No')


n = int(input())
p = [int(input()) for i in range(n - 1)]
leafs = []
for i in range(1, n + 1):
	if i not in p:
		leafs.append(i)
l = []
for (i, x) in enumerate(p):
	if i + 2 in leafs:
		l.append(x)
a = []
for i in p:
	a.append(l.count(i))
print('YES' if min(a) >= 3 else 'NO')


n = int(input())
dic = {}
l_del = []
for i in range(2, n + 1):
	x = int(input())
	dic[i] = x
	if x in dic.keys():
		del dic[x]
		l_del.append(x)
l = list(dic.values())
l_del = list(set(l_del))
for i in l_del:
	if i not in l:
		l.append(i)
l.sort()
judge = 0
z = 1
for i in l:
	if i == z:
		judge = judge + 1
		z = i
	elif i != z:
		if judge >= 3:
			z = i
			judge = 1
		else:
			judge = -1
			break
if judge >= 3:
	print('YES')
else:
	print('NO')


n = int(input())
d = {1: []}
for i in range(n - 1):
	parent = int(input())
	if parent in d:
		d[parent].append(i + 2)
	else:
		d[parent] = [i + 2]
for i in d:
	count = 0
	for j in d[i]:
		if j not in d:
			count += 1
	if count < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
adj = [[] for i in range(n + 1)]
for i in range(2, n + 1, 1):
	adj[int(input())].append(i)
for row in adj:
	if not row:
		continue
	cnt = 0
	for v in row:
		if not adj[v]:
			cnt += 1
		if cnt == 3:
			break
	else:
		print('NO')
		break
else:
	print('YES')


n = int(input())
d = {}
for i in range(1, n):
	buf = int(input())
	if buf in d.keys():
		d[buf].append(i + 1)
	else:
		d.update({buf: [i + 1]})
ans = 'Yes'
for i in d:
	cnt = 0
	for j in d[i]:
		if j not in d.keys():
			cnt += 1
	if cnt < 3:
		ans = 'No'
		break
print(ans)


n = int(input())
L = []
for i in range(n):
	L.append([])
for i in range(1, n):
	L[int(input()) - 1].append(i)
bad = False
for i in L:
	if len(i) > 0:
		s = 0
		for j in i:
			if len(L[j]) == 0:
				s += 1
		if s < 3:
			bad = True
if bad:
	print('No')
else:
	print('Yes')


import sys
sys.setrecursionlimit(10000)
n = int(input())
vs = {}
for c in range(2, n + 1):
	p = int(input())
	if p in vs:
		vs[p].append(c)
	else:
		vs[p] = [c]

def check(n):
	if n not in vs:
		return 1
	ch = vs[n]
	s = 0
	for c in ch:
		r = check(c)
		if r == -1:
			return -1
		s += r
	if s >= 3:
		return 0
	return -1
print('Yes' if check(1) == 0 else 'No')


import sys
readline = sys.stdin.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nn(n):
	return [int(readline()) for i in range(n)]

def nnp(n, x):
	return [int(readline()) + x for i in range(n)]

def nmp(n, x):
	return (int(readline()) + x for i in range(n))

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sl():
	return [s for s in readline().split()]

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
n = i1()
arr = nn(n - 1)
children = [[] for i in range(n + 1)]
i = 2
for p in arr:
	children[p].append(i)
	i += 1
for i in range(1, n + 1):
	if children[i]:
		cnt = 0
		for ch in children[i]:
			if children[ch]:
				continue
			cnt += 1
		if cnt < 3:
			print('NO')
			break
else:
	print('YES')


n = int(input())
par = [-1 for i in range(n + 5)]
for i in range(2, n + 1):
	par[i] = int(input())
is_leaf = [True for i in range(n + 5)]
for i in range(2, n + 1):
	is_leaf[par[i]] = False
chi = [0 for i in range(n + 5)]
for i in range(2, n + 1):
	chi[par[i]] += is_leaf[i]
ans = True
for i in range(1, n + 1):
	if is_leaf[i]:
		continue
	ans &= chi[i] >= 3
print('Yes' if ans else 'No')


n = int(input())
lst = [[] for i in range(n)]
for i in range(n - 1):
	k = int(input())
	lst[k - 1].append(i + 1)
kek = [0 for i in range(n)]
for i in range(n):
	if len(lst[i]) == 0:
		kek[i] = -1
for i in range(n):
	if kek[i] != -1:
		kek[i] = 0
		for j in lst[i]:
			if kek[j] == -1:
				kek[i] += 1
for i in range(n):
	if kek[i] != -1 and kek[i] < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
p = [int(input()) for i in range(n - 1)]
c_children = [0] * (n + 1)
for pi in p:
	c_children[pi] += 1

def is_leaf(i):
	return c_children[i] == 0
for i in range(1, n + 1):
	if not is_leaf(i):
		t = 0
		for j in range(2, n + 1):
			if p[j - 2] == i and is_leaf(j):
				t += 1
		if t < 3:
			print('No')
			return
print('Yes')


def isSpruceNode(nodes, nodeIndex):
	if len(nodes[nodeIndex]) >= 3:
		nLeafChild = 0
		for childIndex in nodes[nodeIndex]:
			if len(nodes[childIndex]) == 0:
				nLeafChild += 1
				if nLeafChild == 3:
					return True
	return False
nNodes = int(input())
nodes = [[] for i in range(nNodes)]
for i in range(1, nNodes):
	nodes[int(input()) - 1].append(i)
allSpruceNodes = True
for nodeIndex in range(0, nNodes):
	if len(nodes[nodeIndex]) != 0:
		if not isSpruceNode(nodes, nodeIndex):
			print('No')
			allSpruceNodes = False
			break
if allSpruceNodes:
	print('Yes')


n = int(input())
g = [[] for i in range(n)]
for i in range(1, n):
	x = int(input())
	g[x - 1].append(i)
leaves = set()
for i in range(n):
	if not g[i]:
		leaves.add(i)
for v in range(n):
	if v not in leaves:
		cnt = 0
		for u in g[v]:
			if u in leaves:
				cnt += 1
		if cnt < 3:
			print('No')
			exit()
print('Yes')


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
N = int(input())
children = collections.defaultdict(set)
leaves = {i for i in range(1, N + 1)}
for u in range(2, N + 1):
	v = int(input())
	children[v].add(u)
	leaves.discard(v)
for (v, us) in children.items():
	if us and len({u for u in us if u in leaves}) < 3:
		print('NO')
		exit(0)
print('YES')


n = int(input())
p = [0, 0]
for i in range(n - 1):
	p.append(int(input()))
num_child = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	num_child[i] = p.count(i)
flag = 0
for i in range(1, n + 1):
	if 0 < num_child[i] < 3:
		print('No')
		flag = 1
		break
	elif num_child[i] >= 3:
		num_leaf = 0
		for j in range(1, n + 1):
			if p[j] == i and num_child[j] == 0:
				num_leaf += 1
		if num_leaf < 3:
			print('No')
			flag = 1
			break
if flag == 0:
	print('Yes')


n = int(input())
p = []
for _ in range(n - 1):
	temp = int(input()) - 1
	p.append(temp)
leaves = []
for i in range(n):
	if i not in p:
		leaves.append(i)
lp = []
for (index, value) in enumerate(p):
	if index + 1 in leaves:
		lp.append(value)
x = min((lp.count(i) for i in p))
print('Yes' if x >= 3 else 'No')


n = int(input())
arr = [0 for i in range(n + 1)]
tb = [0, 0]
for i in range(2, n + 1):
	tb.append(int(input()))
	arr[tb[i]] += 1
dup = list(tb)
tb = list(set(tb))
c = 0
tb = tb[::-1]
for i in tb[:-1]:
	arr[dup[i]] -= 1
	if arr[i] < 3:
		c = 1
		break
if c == 0:
	print('Yes')
else:
	print('No')


n = int(input())
g = [[] for i in range(n)]
for i in range(n - 1):
	v = int(input())
	g[i + 1].append(v - 1)
	g[v - 1].append(i + 1)
for i in range(n):
	cnt = 0
	for u in g[i]:
		if len(g[u]) == 1:
			cnt += 1
	if not len(g[i]) == 1 and (not cnt >= 3):
		print('No')
		exit()
print('Yes')


n = int(input())
f = [[] for i in range(n + 1)]
l = [0 for i in range(n + 1)]
for i in range(n - 1):
	s = int(input())
	f[s].append(i + 2)
	l[s] += 1
b = False
for i in range(n):
	if l[n - i] == 0:
		pass
	else:
		count = 0
		for j in f[n - i]:
			if l[j] == 0:
				count += 1
		if count < 3:
			break
	if i == n - 1:
		b = True
if b:
	print('YES')
else:
	print('NO')


n = int(input())
tree = dict()
for i in range(n - 1):
	t = int(input()) - 1
	if t not in tree:
		tree[t] = set()
	tree[t].add(i + 1)
for v in tree:
	k = 0
	for u in tree[v]:
		if u not in tree:
			k += 1
	if k < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
tree = {1: []}
for node in range(n - 1):
	c = int(input())
	tree[c] += [node + 2]
	tree[node + 2] = []
for (key, value) in tree.items():
	leafs = 0
	if not value:
		continue
	for node in value:
		if not tree[node]:
			leafs += 1
	if leafs < 3:
		print('No')
		break
else:
	print('Yes')


n = int(input())
a = []
par = []
noleaf = []
ch = []
dic = {}
for i in range(1, n):
	x = int(input())
	if x not in dic:
		dic[x] = [i + 1]
	else:
		dic[x].append(i + 1)
o = True
for i in dic:
	count = 0
	for j in dic[i]:
		if j not in dic:
			count += 1
	if count < 3:
		o = False
if o:
	print('Yes')
else:
	print('No')


n = int(input())
a = []
a2 = []
for i in range(n):
	a.append([])
	a2.append([0])
for i in range(n - 1):
	a1 = int(input())
	a[a1 - 1].append(i + 2)
	for g in a:
		if a1 in g:
			g.remove(a1)
			a2[a.index(g)][0] -= 1
o = 'Yes'
for i in a:
	if 0 < len(i) < 3 or (len(i) == 0 and a2[a.index(i)][0] < 0):
		o = 'No'
print(o)


n = int(input())
p = [int(input()) - 1 for _ in range(n - 1)]
leafs = list(filter(lambda x: not x in p, range(n)))
lp = [x for (i, x) in enumerate(p) if i + 1 in leafs]
x = min((lp.count(k) for k in p))
print('Yes' if x >= 3 else 'No')


n = int(input())
tree = []
for i in range(n):
	tree.append([])
for i in range(n - 1):
	tree[int(input()) - 1].append(i + 1)
ans = True
for i in tree:
	now = 0
	if len(i) != 0:
		for j in i:
			if len(tree[j]) == 0:
				now += 1
		if now < 3:
			ans = False
			break
if ans:
	print('Yes')
else:
	print('No')


n = int(input())
d = {}
for i in range(2, n + 1):
	x = int(input())
	if x not in d:
		d[x] = set()
	d[x].add(i)
for i in d:
	ans = 0
	for j in d[i]:
		if j not in d:
			ans += 1
	if ans < 3:
		print('NO')
		exit()
print('YES')


from collections import defaultdict
G = defaultdict(list)

def addEdge(a, b):
	G[a].append(b)
	G[b].append(a)
N = int(input())
for i in range(N - 1):
	v = int(input())
	addEdge(i + 2, v)
leaf = {}
for (i, j) in G.items():
	if len(j) == 1:
		leaf[i] = 1
f = 0
for (i, j) in G.items():
	if leaf.get(i, -1) == -1:
		count = 0
		for k in j:
			if leaf.get(k, -1) == 1:
				count += 1
		if count < 3:
			f = 1
			break
if f == 0:
	print('Yes')
else:
	print('No')


n = int(input())
m = [[] for _ in range(n)]
for i in range(1, n):
	j = int(input()) - 1
	m[j].append(i)

def dfs(i):
	c = 0
	for j in m[i]:
		c += not m[j]
		if m[j]:
			dfs(j)
	if c < 3:
		print('No')
		exit(0)
dfs(0)
print('Yes')


n = int(input())
parent = [[] for _ in range(n + 1)]
for i in range(1, n):
	par = int(input())
	parent[par].append(i + 1)
for childArr in parent:
	leafCounter = 0
	for node in childArr:
		if len(parent[node]) == 0:
			leafCounter += 1
	if len(childArr) > 0 and leafCounter < 3:
		print('No')
		exit()
print('Yes')


import sys
from collections import deque
s = sys.stdin
n = int(s.readline().split()[0])
tree = [[] for j in range(n)]
for j in range(n - 1):
	child = j + 1
	parent = int(s.readline().split()[0]) - 1
	tree[parent].append(child)

def bfs(tree):
	queue = deque([0])
	result = True
	while len(queue) > 0:
		u = queue.popleft()
		leaf_count = 0
		for v in tree[u]:
			if len(tree[v]) == 0:
				leaf_count += 1
			else:
				queue.append(v)
		result = result and leaf_count > 2
		if not result:
			return result
	return result
result = bfs(tree)
if result:
	print('Yes')
else:
	print('No')


n = int(input())
a = [int(input()) - 1 for _ in range(n - 1)]
b = list(filter(lambda x: not x in a, range(n)))
c = [x for (i, x) in enumerate(a) if i + 1 in b]
x = min((c.count(k) for k in a))
print('Yes' if x >= 3 else 'No')


n = int(input())
g = [[] for _ in range(n)]
for v in range(1, n):
	g[int(input()) - 1].append(v)
print(('Yes', 'No')[any((e and sum((not g[v] for v in e)) < 3 for e in g))])


from collections import defaultdict as dd, deque
n = int(input())
V = [i + 1 for i in range(n)]
E = dd(list)
for i in V[1:]:
	p = int(input())
	E[p].append(i)
for v in V:
	if len(E[v]) != 0 and sum((len(E[u]) == 0 for u in E[v])) < 3:
		print('No')
		break
else:
	print('Yes')


from collections import defaultdict
n = int(input())
vert = defaultdict(list)
parents = set()
for i in range(1, n):
	item = int(input())
	if item != 1:
		parents.add(item)
	vert[item].append(i + 1)
result = True
for (par, childs) in vert.items():
	if len(set(childs) - parents) < 3:
		result = False
		break
if result:
	print('Yes')
else:
	print('No')


nei = list(list())
leaf = list()
for i in range(1005):
	nei.append([])
for i in range(1005):
	leaf.append(False)
n = int(input())
for i in range(n - 1):
	r = int(input())
	r -= 1
	nei[r].append(i + 1)
for i in range(n):
	if len(nei[i]) == 0:
		leaf[i] = True
for i in range(n):
	if leaf[i]:
		continue
	cnt = 0
	for j in range(len(nei[i])):
		cnt += leaf[nei[i][j]]
	if cnt < 3:
		print('NO')
		exit()
print('YES')


from collections import defaultdict
d = defaultdict(list)
flag = True
for i in range(int(input()) - 1):
	x = int(input())
	d[x].append(i + 2)
for i in d:
	temp = 0
	for j in d[i]:
		if j not in d:
			temp += 1
	if temp < 3:
		flag = False
if flag == True:
	print('Yes')
else:
	print('No')


l = [[] for i in range(1005)]
f = [0] * 1005
f[1] = 1
n = int(input())
for i in range(2, n + 1):
	x = int(input())
	f[x] = 1
	l[x].append(i)
for i in range(1, n + 1):
	if not f[i]:
		continue
	cnt = 0
	for ele in l[i]:
		if not f[ele]:
			cnt += 1
	if cnt < 3:
		print('No')
		break
else:
	print('Yes')


def wczytywanie():
	n = int(input())
	graf = {}
	for i in range(2, n + 1):
		tata = int(input())
		if tata not in graf:
			graf[tata] = []
		graf[tata].append(i)
	return graf

def czy_swierk(graf):
	for w in graf:
		ile_lisci = 0
		for d in graf[w]:
			if d not in graf:
				ile_lisci += 1
		if ile_lisci < 3:
			return 'No'
	return 'Yes'
print(czy_swierk(wczytywanie()))


n = int(input())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(2, n + 1):
	a[i] = int(input())
	b[a[i]] += 1
for i in range(1, n + 1):
	if b[i] == 0:
		c[a[i]] += 1
for i in range(1, n + 1):
	if b[i] != 0 and c[i] < 3:
		print('NO')
		exit()
print('YES')


n = int(input())
l = [0] * n
for i in range(1, n):
	l[i] = int(input()) - 1
leaves = [1] * n
leaves[0] = 0
for i in range(n):
	for j in range(i, n - 1):
		if l[j] == i:
			leaves[i] = 0
spruce = 'Yes'
for i in range(n):
	if leaves[i] == 0:
		count = 0
		for j in range(i, n):
			if l[j] == i and leaves[j]:
				count += 1
		if count < 3:
			spruce = 'No'
print(spruce)


n = int(input())
c = [0] * (n + 1)
p = [0] * (n + 1)
for i in range(n - 1):
	thing = int(input())
	c[thing] += 1
	p[i + 2] = thing
ans = 'Yes'
for i in range(n + 1):
	if c[i] != 0:
		cnt = 0
		for j in range(n + 1):
			if p[j] == i and c[j] == 0:
				cnt += 1
		if cnt < 3:
			ans = 'No'
print(ans)


def browse(d, key):
	if d[key]['len'] < 3:
		print('No')
		exit()
	for i in range(d[key]['len']):
		new_key = d[key]['a'][i]
		if new_key in d:
			d[key]['len'] -= 1
			if d[key]['len'] < 3:
				print('No')
				exit()
			browse(d, new_key)
n = int(input())
d = {}
for i in range(1, n):
	cur = int(input())
	if cur not in d:
		d[cur] = {'a': [i + 1], 'len': 1}
	else:
		d[cur]['a'] += [i + 1]
		d[cur]['len'] += 1
browse(d, 1)
print('Yes')


n = int(input())
arr = [0] * n
arr[0] = 1
from collections import Counter
for i in range(1, n):
	ini = int(input())
	arr[i] = ini
	arr[ini - 1] = ini
x = Counter(arr)
lis = x.keys()
flag = 0
for i in lis:
	if x[i] <= 3:
		print('NO')
		flag = 1
		break
if flag == 0:
	print('YES')


vertices = int(input())
dicti = {}
vertix = [[] for j in range(vertices + 1)]
for i in range(2, vertices + 1):
	m = int(input())
	vertix[m].append(i)
flag = True
for i in vertix:
	count = 0
	for v in i:
		if len(vertix[v]) == 0:
			count += 1
	if len(i) != 0 and count < 3:
		print('NO')
		flag = False
		break
if flag:
	print('YES')


n = int(input())
a = [0] * n
deg = [0] * n
sons_leaves = [0] * n
for i in range(1, n):
	inp = int(input())
	a[i] = inp - 1
	deg[a[i]] = deg[a[i]] + 1
for i in range(n):
	if deg[i] == 0:
		sons_leaves[a[i]] = sons_leaves[a[i]] + 1
flag = 0
for i in range(n):
	if deg[i] > 0 and sons_leaves[i] < 3:
		print('No')
		flag = 1
		break
if flag == 0:
	print('Yes')


n = int(input())
table = {}
for i in range(2, n + 1):
	parent = int(input())
	if parent in table:
		table[parent].append(i)
	else:
		table[parent] = [i]
flag = True
for k in sorted(table.keys()):
	leaf = 0
	for c in table[k]:
		if not c in table:
			leaf += 1
	if leaf < 3:
		flag = False
		break
print('Yes') if flag else print('No')


n = int(input())
g = {}
for i in range(1, n):
	p = int(input())
	if g.get(p):
		g[p].append(i + 1)
	else:
		g[p] = [i + 1]
ams = 'YES'
for i in g:
	c = 0
	for j in g[i]:
		if j not in g:
			c += 1
	if c < 3:
		ams = 'NO'
print(ams)


n = int(input())
a = [0 for i in range(n)]
for i in range(1, n):
	a[i] = int(input())
h = [[0, 0] for i in range(n + 1)]
for i in range(1, n):
	if a[i] != 1 and h[a[i]][1] == 0:
		h[a[a[i] - 1]][0] -= 1
		h[a[i]][1] = 1
	h[a[i]][0] += 1
c = 0
for i in range(n + 1):
	if h[i][0] < 3 and h[i][0] > 0:
		c = 1
		print('No')
		break
	if h[i][0] == 0 and h[i][1] == 1:
		c = 1
		print('No')
		break
if c == 0:
	if h[1][0] < 3:
		print('No')
	else:
		print('Yes')


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	n = inp()
	children = []
	for _ in range(n):
		children.append([])
	for i in range(n - 1):
		node = inp() - 1
		children[node].append(i + 1)
	for node in range(n):
		if len(children[node]) > 0:
			count = 0
			for ch in children[node]:
				if len(children[ch]) == 0:
					count += 1
			if count < 3:
				print('No')
				return
	print('Yes')
BUFSIZE = 8192

def inp():
	return int(input())

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

def insr():
	return input().strip()

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

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

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
out = [0 for i in range(n + 1)]
cnt = [0 for i in range(n + 1)]
p = [-1 for i in range(n + 1)]
for i in range(2, n + 1):
	p[i] = int(input())
	out[p[i]] += 1
for i in range(1, n + 1):
	if out[i] == 0:
		cnt[p[i]] += 1
for i in range(1, n + 1):
	if out[i] != 0 and cnt[i] < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
children = {}
for vertex in range(n):
	children[vertex] = []
for vertex in range(n - 1):
	children[int(input()) - 1].append(vertex + 1)
spruce = True
for vertex in range(n):
	num_leaves = 0
	for child in children[vertex]:
		if len(children[child]) == 0:
			num_leaves += 1
	if len(children[vertex]) != 0 and num_leaves < 3:
		spruce = False
print('Yes' if spruce else 'No')


from itertools import permutations
MOD = 10 ** 9 + 7

def main():
	n = int(input())
	parent = [0] * n
	is_leaf = [True] * n
	for i in range(1, n):
		parent[i] = int(input()) - 1
		is_leaf[parent[i]] = False
	leafs_cnt = [0] * n
	for i in range(n):
		leafs_cnt[parent[i]] += is_leaf[i]
	for i in range(n):
		if not is_leaf[i] and leafs_cnt[i] < 3:
			print('No')
			return
	print('Yes')
while 1:
	main()
	break


def dfs(s, prev_leaf, leaf):
	if prev_leaf:
		leaf_child[s] += 1
	for x in g[s]:
		dfs(x, leaf, False)
n = int(input())
leaf_child = [0] * (n + 1)
g = {x: set() for x in range(1, n + 1)}
is_leaf = [1] * (n + 1)
for x in range(n - 1):
	s = int(input())
	g[x + 2].add(s)
	is_leaf[s] = 0
for x in range(2, n + 1):
	if g[x] != set() and is_leaf[x]:
		dfs(x, False, True)
b = True
for x in range(1, n + 1):
	if not is_leaf[x]:
		if leaf_child[x] < 3:
			b = False
print('Yes' if b else 'No')


from collections import *
import math
from sys import *
inp = stdin.readline
str = str
flag = 1
if not flag:
	out = stdout.write
else:
	out = print

def linp(x):
	return list(map(int, x.split()))

def minp(x):
	return map(int, x.split())
n = int(inp())
dic = defaultdict(list)
ii = 1
while n - 1 > 0:
	dic[int(inp())].append(ii + 1)
	ii += 1
	n -= 1
flag = 0
ans = {key: 0 for key in dic}
for i in dic:
	for j in dic[i]:
		if j not in dic:
			ans[i] += 1
for (i, v) in ans.items():
	if v < 3:
		out('No')
		break
else:
	out('Yes')


n = int(input())
rec = [0] * n
P = {}
for i in range(1, n):
	a = int(input())
	P[i] = a - 1
	rec[a - 1] += 1
for i in range(n):
	if rec[i] > 0:
		tmp = 0
		for j in range(n):
			if i == j:
				continue
			if rec[j] == 0 and j in P and (P[j] == i):
				tmp += 1
		if tmp < 3:
			print('No')
			exit()
print('Yes')


from collections import deque
n = int(input())
graph = []
for i in range(n + 1):
	graph.append([])
parent = [-1, -1]
for j in range(2, n + 1):
	p = int(input())
	parent.append(p)
	graph[p].append(j)

def bfs(graph, node, visited, leafs, parent):
	q = deque()
	visited[node] = True
	q.append(node)
	while q:
		node = q.popleft()
		if not graph[node]:
			leafs[parent[node]] += 1
		else:
			for i in graph[node]:
				if not visited[i]:
					visited[i] = True
					q.append(i)
leafs = [0] * (n + 1)
bfs(graph, 1, [False] * (n + 1), leafs, parent)
for i in range(len(leafs)):
	if graph[i]:
		if leafs[i] < 3:
			print('NO')
			exit()
print('YES')


class Node:

	def __init__(self, index):
		self.parent = None
		self.ancestor = []
		self.end = 1
		self.index = index
narr = []
n = int(input())
for i in range(n):
	narr.append(Node(i))
for i in range(1, n):
	par = int(input()) - 1
	narr[i].parent = narr[par]
	narr[par].ancestor.append(narr[i])
	narr[par].end = 0
varr = [0] * n

def f(node):
	if node == None:
		return
	if varr[node.index]:
		return
	counter = 0
	for a in node.ancestor:
		if a.end:
			counter += 1
	if counter < 3:
		print('No')
		quit()
	varr[node.index] = 1
	f(node.parent)
for node in narr:
	if node.end:
		f(node.parent)
print('Yes')


n = int(input())
leaf = [True] * (n + 1)
chd = [-1] * (n + 1)
for i in range(2, n + 1):
	p = int(input())
	if chd[p] == -1:
		chd[p] = [i]
		leaf[p] = False
	else:
		chd[p].append(i)
suc = True
for i in range(1, n + 1):
	ctr = 0
	if not leaf[i]:
		for x in chd[i]:
			if leaf[x]:
				ctr += 1
		if ctr < 3:
			suc = False
			break
if suc:
	print('Yes')
else:
	print('No')


n = int(input())
dct = {}
child = [None] * (n + 1)
ans = 'Yes'
for i in range(1, n):
	x = int(input())
	if child[x] == None:
		child[x] = [i + 1]
	else:
		child[x].append(i + 1)
for i in child:
	if i != None:
		cnt = 0
		for j in i:
			if child[j] == None:
				cnt += 1
		if cnt < 3:
			ans = 'No'
			break
print(ans)


n = int(input().strip())
(p, deg) = ([0] * (n + 1), [0] * (n + 1))
for i in range(2, n + 1):
	p[i] = int(input())
	deg[p[i]] += 1
leaf_childs = [0] * (n + 1)
for i in range(1, n + 1):
	if deg[i] == 0:
		leaf_childs[p[i]] += 1
for i in range(1, n + 1):
	if deg[i] > 0 and leaf_childs[i] < 3:
		print('No')
		quit()
print('Yes')


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 7)

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

def lin():
	return list(map(int, stdin.readline().split()))
n = iin()
adj = [[] for i in range(n)]
for i in range(n - 1):
	b = iin()
	adj[b - 1].append(i + 1)
lf = set()
for i in range(n):
	if adj[i] == []:
		lf.add(i)
for i in range(n):
	ch = 0
	if i in lf:
		continue
	for j in adj[i]:
		if j in lf:
			ch += 1
	if ch < 3:
		print('No')
		exit()
print('Yes')


from sys import stdin, stdout
label = 1

def dfs(v):
	global label
	if not len(vertices[v]):
		return 1
	else:
		cnt = 0
		for u in vertices[v]:
			cnt += dfs(u)
		if cnt < 3:
			label = 0
		return 0
n = int(stdin.readline())
vertices = [[] for i in range(n + 10)]
parents = [0 for i in range(n + 10)]
for i in range(1, n):
	parents[i + 1] = int(stdin.readline())
	vertices[parents[i + 1]].append(i + 1)
dfs(1)
if label:
	stdout.write('Yes')
else:
	stdout.write('No')


n = int(input())
arr = [0] * (n + 2)
arri = [False] * (n + 2)
itrmap = dict()
mitrmap = dict()
for i in range(n - 1):
	inp = int(input())
	arr[i + 2] = inp
	try:
		itrmap[inp].append(i + 2)
		mitrmap[inp] += 1
	except:
		itrmap[inp] = [i + 2]
		mitrmap[inp] = 1
	try:
		if arri[inp] == False:
			mitrmap[arr[inp]] -= 1
			arri[inp] = True
	except:
		pass
maplist = list(mitrmap.keys())
flag = 0
for j in maplist:
	if mitrmap[j] < 3:
		flag = 1
		break
if flag == 1:
	print('No')
else:
	print('Yes')


from collections import defaultdict
edges = defaultdict(list)
V = int(input())
for vertex in range(2, V + 1):
	parent = int(input())
	edges[parent].append(vertex)

def is_spruce(parent):
	leafs = 0
	for child in edges[parent]:
		if child in edges:
			if not is_spruce(child):
				return False
		else:
			leafs += 1
	return leafs >= 3
print('Yes' if is_spruce(1) else 'No')


def result(node):
	for i in node:
		if len(i):
			children = 0
			for j in i:
				if not len(node[j]):
					children += 1
			if children < 3:
				print('No')
				return
	print('Yes')
n = int(input())
node = [[] for _ in range(n)]
for i in range(1, n):
	a = int(input()) - 1
	node[a].append(i)
result(node)


def spruce():
	n = int(input())
	tree = dict()
	parent = set()
	for i in range(n - 1):
		p = int(input())
		parent.add(p)
		childs = tree.get(p, list())
		childs.append(i + 2)
		tree[p] = childs
	is_spruce = True
	for p in parent:
		childs = set(tree[p])
		leafs = childs - parent
		if len(leafs) < 3:
			is_spruce = False
			break
	if is_spruce:
		print('Yes')
	else:
		print('No')
spruce()


n = int(input())
x = [0] * n
parent = [False] * n
parent_child = [0] * n
for i in range(1, n):
	t = int(input())
	x[i] = t - 1
	parent[t - 1] = True
for i in range(1, n):
	if parent[i] is False:
		parent_child[x[i]] += 1
flag = False
for i in range(0, n):
	if parent[i] and parent_child[i] < 3:
		flag = True
if flag:
	print('No')
else:
	print('Yes')


n = int(input())
tree = [[0] for i in range(n + 1)]
for i in range(n - 1):
	p = int(input())
	tree[p][0] += 1
	tree[p].append(i + 2)
spruce = 1
for i in range(1, n + 1):
	if tree[i][0] > 0:
		if len(tree[i]) < 4:
			spruce = 0
			break
		cnt = 0
		for j in tree[i][1:]:
			if tree[j][0] == 0 or len(tree[j]) == 1:
				cnt += 1
		if cnt < 3:
			spruce = 0
			break
if spruce:
	print('Yes')
else:
	print('No')


n = int(input())
a = []
for i in range(n):
	a.append([-1])
flak = 0
for i in range(n - 1):
	m = int(input())
	a[m - 1].append(i + 1)
for i in range(n):
	s = 0
	for j in range(1, len(a[i])):
		if len(a[a[i][j]]) == 1:
			s += 1
	if s < 3 and len(a[i]) != 1:
		flak = 1
if flak == 0:
	print('Yes')
else:
	print('No')


n = int(input())
d = {}
dic = {}
for i in range(1, n):
	x = int(input())
	if not x in d:
		d[x] = []
	d[x].append(i + 1)
	dic[i + 1] = x
	if x > 1:
		if x in d[dic[x]]:
			d[dic[x]].remove(x)
flag = True
for x in d:
	if len(d[x]) < 3:
		flag = False
		break
if flag:
	print('Yes')
else:
	print('No')


n = int(input())
d = {}
for i in range(n - 1):
	k = int(input())
	if k not in d:
		d[k] = [i + 2]
	else:
		d[k].append(i + 2)
flag = 0
for i in d:
	w = 0
	for j in d[i]:
		if j not in d:
			w += 1
	if w < 3:
		flag = 1
		break
if flag == 0:
	print('Yes')
else:
	print('No')


n = int(input())
deg = [0 for _ in range(n + 1)]
chd = [0 for _ in range(n + 1)]
f = [0 for _ in range(n + 1)]
for i in range(2, n + 1):
	f[i] = int(input())
	deg[f[i]] += 1
isok = True
for i in range(2, n + 1):
	if deg[i] == 0:
		chd[f[i]] += 1
for i in range(1, n + 1):
	if deg[i] != 0 and chd[i] < 3:
		isok = False
if isok:
	print('Yes')
else:
	print('No')


n = int(input())
arr = [[0, -1, True]] + [[0, 0, False]] * (n - 1)
for i in range(1, n):
	a = int(input())
	arr[i] = [0, a - 1, False]
	if arr[a - 1][2] == True:
		arr[a - 1] = [arr[a - 1][0] + 1, arr[a - 1][1], True]
	else:
		if arr[a - 1][1] >= 0:
			arr[arr[a - 1][1]][0] -= 1
		arr[a - 1] = [arr[a - 1][0] + 1, arr[a - 1][1], True]
for i in range(n):
	if arr[i][2] == True and arr[i][0] == 0 or (arr[i][0] < 3 and arr[i][0] > 0):
		print('No')
		break
else:
	print('Yes')


n = int(input())
tree = [[] for i in range(n)]
for i in range(1, n):
	tree[int(input()) - 1].append(i)
for children in tree:
	if len(children) != 0:
		leafs = 0
		for child in children:
			if len(tree[child]) == 0:
				leafs += 1
		if leafs < 3:
			print('No')
			break
else:
	print('Yes')


n = int(input())
arr = [[] for i in range(n + 1)]
for i in range(1, n):
	parent = int(input())
	arr[parent].append(i + 1)
flag = True
for i in arr:
	if len(i) == 0:
		continue
	elif len(i) < 3:
		flag = False
		break
	else:
		count = 0
		for j in i:
			if len(arr[j]) == 0:
				count += 1
		if count < 3:
			flag = False
			break
if flag == True:
	print('Yes')
else:
	print('No')


from sys import stdin, stdout
n = int(stdin.readline())
tree = dict()
for i in range(1, n):
	cur = int(stdin.readline())
	if not cur in tree:
		tree[cur] = set()
	tree[cur].add(i + 1)
ans = 'Yes'
for i in tree:
	cnt = 0
	for j in tree[i]:
		if not j in tree:
			cnt += 1
	if cnt < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
l = list()
for i in range(n + 1):
	l.append([])
for i in range(n - 1):
	curr = int(input())
	l[curr].append(i + 2)

def is_leaf(l, vertex):
	if l[vertex] == []:
		return True
	else:
		return False
f = True
for i in range(1, n + 1):
	current_vertex = l[i]
	curr_count = 0
	for child in current_vertex:
		if is_leaf(l, child) is True:
			curr_count += 1
	if curr_count < 3 and is_leaf(l, i) is False:
		f = False
		break
if f is True:
	print('Yes')
else:
	print('No')


n = int(input())
d = {1: []}
for i in range(n - 1):
	l = int(input())
	if l in d:
		d[l].append(i + 2)
	else:
		d[l] = [i + 2]
fl = 0
for j in d:
	if len(d[j]) < 3:
		fl = 1
		break
	else:
		x = 0
		for k in d[j]:
			if k not in d:
				x += 1
			if x == 3:
				break
		else:
			fl = 1
			break
if fl == 1:
	print('No')
else:
	print('Yes')


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

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())
a = matrix(n + 1, 0)
val = vector(n + 1, -1)

def dfs(x, par):
	if len(a[x]) == 0:
		val[par] += 1
	else:
		for i in a[x]:
			dfs(i, x)
for i in range(2, n + 1):
	x = int(input())
	a[x].append(i)
for i in range(1, n + 1):
	if len(a[i]) != 0:
		val[i] = 0
dfs(1, 0)
for i in range(1, n + 1):
	if val[i] != -1 and val[i] < 3:
		print('No')
		sys.exit()
print('Yes')


d = dict()
d[1] = []
n = int(input())
for i in range(2, n + 1):
	u = int(input())
	d[u].append(i)
	d[i] = []

def spruce():
	for i in d:
		c = len(d[i])
		if c == 0:
			continue
		for j in d[i]:
			if len(d[j]) != 0:
				c -= 1
			if c < 3:
				return 'No'
	return 'Yes'
print(spruce())


n = int(input())
a = [0, 0] + [int(input()) for _ in range(n - 1)]
isleaf = [0, 0] + [1 for _ in range(n)]
chleaf = [0, 0] + [0 for _ in range(n)]
for i in range(2, n + 1):
	isleaf[a[i]] = 0
for i in range(2, n + 1):
	if isleaf[i]:
		chleaf[a[i]] += 1
ans = 'Yes'
for i in range(1, n + 1):
	if not isleaf[i] and chleaf[i] < 3:
		ans = 'No'
		break
print(ans)


n = int(input())
l = []
for i in range(n - 1):
	x = int(input())
	l.append(x)
dict = {}
i = 2
for e in l:
	if e not in dict:
		dict[e] = [i]
	else:
		dict[e].append(i)
	i += 1
for (e, f) in dict.items():
	cl = 0
	for u in f:
		if u not in dict:
			cl += 1
	if cl < 3:
		print('NO')
		exit()
print('YES')


from collections import defaultdict as dd
n = int(input())
d = dd(list)
d1 = dd(int)
for i in range(n - 1):
	m = int(input())
	d[m].append(i + 2)
	d1[m] = 1
f = 0
for k in d:
	count = 0
	for c in d[k]:
		if d1[c] == 0:
			count += 1
	if count < 3:
		f = 1
		break
if f == 0:
	print('Yes')
else:
	print('No')


n = int(input().strip())
parents = [0] * (n + 1)
for i in range(2, n + 1):
	parent = int(input().strip())
	parents[i] = parent
tree = {}
for i in range(1, n + 1):
	tree[i] = []
for node in range(1, n + 1):
	for i in range(2, len(parents)):
		if parents[i] == node:
			tree[node].append(i)
for node in range(1, n + 1):
	if len(tree[node]) != 0:
		leaf_childs = 0
		for child in tree[node]:
			if len(tree[child]) == 0:
				leaf_childs += 1
		if leaf_childs < 3:
			print('No')
			quit()
print('Yes')


n = int(input())
a = []
for i in range(n):
	a.append([])
b = [0] * n
for i in range(n - 1):
	z = int(input())
	b[z - 1] += 1
	a[z - 1].append(i + 1)
z = 0
for i in range(n):
	c = 0
	for j in range(len(a[i])):
		if b[a[i][j]] == 0:
			c += 1
	if c >= 3 or len(a[i]) == 0:
		z += 1
if z == n:
	print('Yes')
else:
	print('No')


n = int(input())
graph = []
if n == 1:
	print('No')
for i in range(n):
	graph.append([])
for i in range(n - 1):
	u = int(input())
	graph[u - 1].append(i + 1)
used = [0] * n
used[0] = 1

def dfs(v):
	count = 0
	if len(graph[v]) == 0:
		return
	for child in graph[v]:
		if not used[child]:
			if len(graph[child]) == 0:
				count += 1
			used[child] = 1
			dfs(child)
	if count < 3:
		print('No')
		exit()
dfs(0)
print('Yes')


n = int(input())
par = [-1 for i in range(n + 123)]
for i in range(2, n + 1):
	par[i] = int(input())
is_leaf = [True for i in range(n + 123)]
for i in range(2, n + 1):
	is_leaf[par[i]] = False
children = [0 for i in range(n + 123)]
for i in range(2, n + 1):
	children[par[i]] += is_leaf[i]
ans = True
for i in range(1, n + 1):
	if is_leaf[i]:
		continue
	ans &= children[i] >= 3
print('Yes' if ans else 'No')


n = int(input())
childlists = [[] for i in range(n)]
for i in range(n - 1):
	childlists[int(input()) - 1].append(i + 1)
result = True
for childlist in childlists:
	if not childlist:
		continue
	leafchild_count = 0
	for child in childlist:
		if not childlists[child]:
			leafchild_count += 1
	if leafchild_count < 3:
		result = False
		break
print('Yes' if result else 'No')


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	A = [None] + [[] for _ in range(n)]
	for i in range(2, n + 1):
		p = int(input())
		A[p].append(i)

	def isLeaf(u):
		return A[u] == []

	def isSpurce(u):
		if isLeaf(u):
			return True
		cnt = 0
		for v in A[u]:
			if isLeaf(v):
				cnt += 1
				continue
			if not isSpurce(v):
				return False
		return cnt > 2
	print('YES' if isSpurce(1) else 'No')
main()


n = int(input())
v = [0] * n
children = [[] for _ in range(n)]
for i in range(1, n):
	p = int(input())
	v[i] = p - 1
	children[p - 1].append(i)

def check(r):
	if len(children[r]) == 0:
		return True
	leaves = 0
	good = True
	for c in children[r]:
		if len(children[c]) == 0:
			leaves += 1
		good = good and check(c)
	return good and leaves >= 3
if check(0):
	print('Yes')
else:
	print('No')


import sys
readline = sys.stdin.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nn(n):
	return [int(readline()) for i in range(n)]

def nnp(n, x):
	return [int(readline()) + x for i in range(n)]

def nmp(n, x):
	return (int(readline()) + x for i in range(n))

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sl():
	return [s for s in readline().split()]

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
n = i1()
arr = nn(n - 1)
non_leaf = set(arr)
cnt = dict.fromkeys(non_leaf, 0)
for (p, i) in zip(arr[::-1], range(n, 1, -1)):
	if i not in non_leaf:
		cnt[p] += 1
print('YES' if all((i >= 3 for i in cnt.values())) else 'NO')


import sys
from collections import Counter
L = sys.stdin.readlines()
n = int(L[0])
l = list(map(lambda x: int(x), L[1:]))
cnt = Counter(l)
cntl = Counter([l[i - 2] for i in range(2, n + 1) if i not in cnt])
for a in cnt:
	if cntl[a] < 3:
		print('No')
		break
else:
	print('Yes')


def create_graph(n):
	graph = [[] for x in range(0, n)]
	return graph

def children(node):
	if len(graph[node]) == 0:
		leafs[node] = 1
		return
	for x in graph[node]:
		children(x)

def dfs(node):
	b = True
	count = 0
	for x in graph[node]:
		if leafs[x]:
			count = count + 1
		else:
			b = b and dfs(x)
	if count < 3:
		b = False
	return b
nodes = input()
graph = create_graph(int(nodes))
leafs = [0] * int(nodes)
for x in range(2, int(nodes) + 1):
	a = input()
	graph[int(a) - 1].append(x - 1)
children(0)
result = dfs(0)
if result:
	print('Yes')
else:
	print('No')


q = int(input())
graff = []
leaf = [False] * (q + 1)
for i in range(q + 1):
	graff.append([])
for i in range(1, q):
	d = int(input())
	graff[d].append(i + 1)
for i in range(1, q + 1):
	cur = graff[i]
	if len(cur) != 0:
		c = 0
		for j in cur:
			if len(graff[j]) == 0:
				c += 1
		if c < 3:
			print('No')
			break
else:
	print('Yes')


class Vertex:

	def __init__(self, key):
		self.connections = {}
		self.key = key

	def addNeighbor(self, key, weight):
		self.connections[key] = weight

	def edgeLength(self, destination):
		return self.connections[destination.key]

	def isNeighbor(self, destination):
		if type(destination) == int:
			return destination in self.connections
		else:
			return destination.key in self.connections

	def getConnections(self):
		return self.connections.keys()

	def __repr__(self):
		return str(self.key)

class Graph:

	def __init__(self):
		self.verticesList = {}

	def containsVertex(self, key):
		return key in self.verticesList

	def getAllVertices(self):
		return self.verticesList

	def verticesCount(self):
		return len(self.verticesList)

	def addVertex(self, key):
		self.verticesList[key] = Vertex(key)

	def __iter__(self):
		return iter(self.verticesList.values())

	def getAdjacencyList(self, key):
		if type(key) is str:
			return [self.getVertex(val) for val in list(self.getVertex(key).connections.keys())]
		elif type(key) is Vertex:
			return [self.getVertex(val) for val in list(key.connections.keys())]

	def getVertex(self, key):
		return self.verticesList[key]

	def addEdge(self, source, destination, weight):
		if source not in self.verticesList:
			self.verticesList[source] = Vertex(source)
		if destination not in self.verticesList:
			self.verticesList[destination] = Vertex(destination)
		self.verticesList[source].addNeighbor(destination, weight)

	def adj(self):
		return self.verticesList

def buildGraph(graph):
	n = int(input().strip())
	for i in range(2, n + 1):
		v = int(input().strip())
		graph.addEdge(v, i, 1)
g = Graph()
buildGraph(g)
for i in g.verticesList:
	ve = g.getVertex(i)
	adj = g.getAdjacencyList(ve)
	ve.is_leaf = len(adj) == 0
spruce = True
for i in g.verticesList:
	if not g.getVertex(i).is_leaf:
		adj = g.getAdjacencyList(g.getVertex(i))
		c = 0
		for child in adj:
			if child.is_leaf:
				c += 1
		if c < 3:
			spruce = False
		if not spruce:
			break
print('YES' if spruce else 'NO')


n = int(input())
adj = [[] for i in range(n + 5)]
v = [0] * (n + 5)
global count
count = [0] * (n + 5)
l = [0] * (n + 5)

def dfs(adj, u, v, count):
	v[u] = 1
	l[u] = -1
	for i in range(len(adj[u])):
		if len(adj[adj[u][i]]) == 1:
			l[adj[u][i]] = 1
			count[u] += 1
		elif v[adj[u][i]] == 0:
			l[adj[u][i]] = 1
			dfs(adj, adj[u][i], v, count)

def check(count):
	for i in range(len(count)):
		if count[i] > 0 and count[i] < 3:
			return False
		if count[i] == 0 and l[i] == -1:
			return False
	return True
for i in range(2, n + 1):
	u = int(input())
	adj[i].append(u)
	adj[u].append(i)
dfs(adj, 1, v, count)
if check(count):
	print('Yes')
else:
	print('No')


iloW = int(input())
lisc = [True] * (iloW + 1)
rodzic = [0] * (iloW + 1)
for i in range(2, iloW + 1):
	rodzic[i] = int(input())
	lisc[rodzic[i]] = False
ileLisci = [0] * (iloW + 1)
for i in range(2, iloW + 1):
	if lisc[i]:
		ileLisci[rodzic[i]] += 1
spruce = True
for i in range(1, iloW + 1):
	if not lisc[i] and ileLisci[i] < 3:
		spruce = False
		break
if spruce:
	print('Yes')
else:
	print('No')


n = int(input())
g = [[] for _ in range(n)]
for v in range(1, n):
	g[int(input()) - 1].append(v)
r = True
for v in g:
	if len(v) > 0:
		count = 0
		for c in v:
			if not g[c]:
				count += 1
		if count < 3:
			r = False
			break
print(['NO', 'YES'][r])


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

def explore(adj, i):
	count = []
	q = [i]
	while q != []:
		i = min(q)
		q.remove(i)
		if visited[i] != 1:
			visited[i] = 1
			count += [i + 1]
			q += adj[i]
	return count
x = 0
n = inp()
adj = [[] for i in range(n)]
for i in range(n - 1):
	u = inp()
	if u != -1 and u - 1 != i + 1:
		adj[u - 1].append(i + 1)
for i in range(n):
	if adj[i] == []:
		continue
	else:
		a = 0
		for j in adj[i]:
			if adj[j] == []:
				a += 1
		if a < 3:
			x = 1
			break
if x == 1:
	print('NO')
else:
	print('YES')


nodes_size = int(input())
nodes = dict()
for i in range(nodes_size - 1):
	parent = int(input())
	nodes[parent] = nodes.get(parent, [])
	nodes[parent].append(i + 2)
for i in nodes:
	leaves = 0
	for children in nodes[i]:
		if nodes.get(children, 0) == 0:
			leaves += 1
		else:
			pass
	if leaves < 3:
		print('No')
		break
else:
	print('Yes')


n = int(input())
v = [[] for i in range(n)]
b = [False] * n
for i in range(1, n):
	x = int(input()) - 1
	v[x] += [i]
for i in range(n):
	c = 0
	for q in v[i]:
		if not v[q]:
			c += 1
	if c > 2:
		b[i] = True
ans = 'Yes'
for i in range(n):
	if not b[i] and v[i]:
		ans = 'No'
print(ans)


from math import sqrt
R = lambda : tuple(map(int, input().split(' ')))

def main():
	n = int(input())
	tree = {}
	(leaves, parents) = (set(), set())
	for i in range(1, n):
		p = int(input())
		tree.setdefault(p, []).append(i + 1)
		parents.add(p)
		leaves.discard(p)
		leaves.add(i + 1)
	for node in parents:
		leaf_count = 0
		for i in tree[node]:
			if i in leaves:
				leaf_count += 1
		if leaf_count < 3:
			print('No')
			return
	print('Yes')
main()


n = int(input())
tree = dict()
for i in range(1, n + 1):
	tree[i] = list()
for i in range(2, n + 1):
	tree[int(input())].append(i)
flag = 'YES'
for (a, b) in tree.items():
	if len(b) > 0 and len(b) < 3:
		flag = 'NO'
		break
	if len(b) != 0:
		cnt = 0
		for i in b:
			if len(tree[i]) == 0:
				cnt += 1
		if cnt < 3:
			flag = 'NO'
			break
print(flag)


n = int(input())
p = [int(input()) for _ in range(n - 1)]
c = [[] for _ in range(n)]
l = [0 for _ in range(n)]
for i in range(n - 1):
	c[p[i] - 1].append(i + 1)
for i in range(n - 1):
	for u in c[i]:
		if len(c[u]) == 0:
			l[i] += 1
if any(map(lambda i: 0 < len(c[i]) and l[i] < 3, range(n))):
	ans = 'No'
else:
	ans = 'Yes'
print(ans)


for _ in range(1):
	n = int(input())
	flag = 1
	p = []
	for i in range(n - 1):
		p.append(int(input()))
	q = list(set(p))
	l = [0] * (n + 2)
	for i in q:
		if i != 1:
			l[p[i - 2]] -= 1
	for i in p:
		l[i] += 1
	for i in q:
		if l[i] < 3:
			flag = 0
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
n = int(input())
d = {}
l = set()
for i in range(2, n + 1):
	x = int(input())
	if x in d:
		d[x].add(i)
	else:
		d[x] = {i}
		l.add(x)
flag = 'YES'
for i in d:
	count = 0
	for k in d[i]:
		if k not in l:
			count += 1
	if count < 3:
		flag = 'NO'
print(flag)


n = int(input())
G = {i: [] for i in range(n)}
for i in range(n - 1):
	x = int(input())
	G[x - 1].append(i + 1)
inv = True

def bfs():
	global inv
	visited = [False for i in range(n)]
	q = []
	q.append(0)
	while q != []:
		x = q.pop(0)
		y = G[x]
		if visited[x] == False:
			if y != []:
				count = 0
				for i in y:
					if G[i] == []:
						count += 1
					q.append(i)
				if count < 3:
					inv = False
					return
			visited[x] = True
	return
bfs()
if inv:
	print('Yes')
else:
	print('No')


n = int(input())
cache = {}
for i in range(n - 1):
	p = int(input())
	if p in cache:
		cache[p].append(i + 2)
	else:
		for j in cache:
			if p in cache[j]:
				cache[j].remove(p)
		cache[p] = [i + 2]
ans = 'Yes'
for i in cache:
	if len(cache[i]) < 3:
		ans = 'No'
		break
print(ans)


n = int(input())
a = [int(input()) for i in range(n - 1)]
c = [[] for i in range(n)]
for i in range(n - 1):
	c[a[i] - 1].append(i + 1)
for i in range(n):
	count = 0
	for j in c[i]:
		if c[j] == []:
			count += 1
	if c[i] != [] and count >= 0 and (count < 3):
		print('No')
		exit()
print('Yes')


n = int(input())
x = [[False, 0] for _ in range(n)]
l = []
l.append(0)
for i in range(n - 1):
	q = int(input()) - 1
	x[q] = [True, 0]
	l.append(q)
for (i, j) in enumerate(x):
	if not j[0]:
		x[l[i]][1] += 1
flag = True
for i in x:
	if i[0] and i[1] < 3:
		flag = False
		break
if flag:
	print('Yes')
else:
	print('No')


import sys

def dfs(v):
	if len(next[v]) == 0:
		leaf[v] = 1
	for u in next[v]:
		dfs(u)
n = int(sys.stdin.readline())
next = dict()
for i in range(1, n + 1):
	next[i] = []
for i in range(n - 1):
	m = int(sys.stdin.readline())
	next[m].append(i + 2)
leaf = [0 for i in range(n + 1)]
dfs(1)
ok = 1
for i in next:
	if len(next[i]) == 0:
		continue
	s = 0
	for t in next[i]:
		s += leaf[t]
	if s < 3:
		ok = 0
print(['No', 'Yes'][ok])


class TreeNode:

	def __init__(self, val):
		self.val = val
		self.children = []

class Solution:

	def __init__(self):
		self.root = TreeNode(1)

	def insertIntoTree(self, indexOfParent, val):
		self.insertHelper(self.root, indexOfParent, val)

	def insertHelper(self, curr, indexOfParent, val):
		if not curr:
			return None
		if curr.val == indexOfParent:
			curr.children.append(TreeNode(val))
			return
		for child in curr.children:
			self.insertHelper(child, indexOfParent, val)

	def isSpruce(self, root):
		self.ans = True
		self.dfs(root)
		return self.ans

	def dfs(self, curr):
		if not curr:
			return None
		if not curr.children:
			return True
		leafCount = 0
		for child in curr.children:
			isLeaf = self.dfs(child)
			leafCount += 1 if isLeaf else 0
		if leafCount < 3:
			self.ans = False
		return False
sol = Solution()
n = int(input().strip())
for i in range(n - 1):
	indexOfParent = int(input().strip())
	sol.insertIntoTree(indexOfParent, 2 + i)
print('Yes' if sol.isSpruce(sol.root) else 'No')


n = int(input())
(d, l, m, a) = ([0] * (n + 1), [0] * 2, [0] * (n + 1), 0)
for _ in range(n - 1):
	a = int(input())
	l.append(a)
	m[a] += 1
for i in range(1, n + 1):
	if m[i] == 0:
		d[l[i]] += 1
for i in range(1, n + 1):
	if m[i] > 0 and d[i] < 3:
		print('No')
		break
else:
	print('Yes')


n = int(input())
l = [[0, 0, 0]]
for i in range(n - 1):
	l.append([int(input()), 0, 0])
for i in range(n - 1, 0, -1):
	if l[i][1] == 0:
		l[l[i][0] - 1][1] += 1
	else:
		l[l[i][0] - 1][2] += 1
f = 1
for i in range(n):
	if l[i][2] > 0 and l[i][1] == 0 and (l[i][1] < 3) or (l[i][1] < 3 and l[i][1] != 0):
		f = 0
		break
if f:
	print('Yes')
else:
	print('No')


node_num = int(input())

class Tree:

	def __init__(self, cargo, *children):
		self.cargo = cargo
		self.children = list(children)
		self.addNode = lambda node: self.children.append(node)

	def __str__(self):
		return str(self.cargo)

def bfs(tree, target):
	q = [tree]
	while q:
		if q[0].cargo == target:
			return q[0]
		if q[0].children:
			for y in q[0].children:
				q.append(y)
		del q[0]
	return False
candidate = Tree(1)
for x in range(node_num - 1):
	parent_index = int(input())
	location = bfs(candidate, parent_index)
	if location:
		location.addNode(Tree(x + 2))
check_q = [candidate]
while check_q:
	leaf = 0
	for x in check_q[0].children:
		if x.children:
			check_q.append(x)
		else:
			leaf += 1
	if leaf < 3:
		print('No')
		exit()
	else:
		del check_q[0]
print('Yes')


n = int(input())
i = 2
edge = dict()
leafs = set()
i = 1
while i <= n:
	edge[i] = set()
	i += 1
i = 2
while i <= n:
	parent = int(input())
	edge[parent].add(i)
	i += 1
dlt = list()
for x in edge.keys():
	if len(edge[x]) == 0:
		leafs.add(x)
		dlt.append(x)
for x in dlt:
	del edge[x]
for x in edge.values():
	cnt = int(0)
	for y in x:
		if y in leafs:
			cnt += 1
	if cnt < 3:
		print('No')
		quit()
print('Yes')
quit()


class tree:

	def __init__(self, index):
		self.children = []
		self.isleaf = True
		self.parent = None
		self.index = index

	def add(self, graph):
		self.children.append(graph)
		graph.parent = self
		self.isleaf = False

	def is_spruce(self):
		ctr = 0
		for el in self.children:
			if el.isleaf:
				ctr += 1
			elif not el.is_spruce():
				return False
		if ctr >= 3:
			return True
		return False

def find_index(root, index):
	if root.index == index:
		return root
	if root.isleaf:
		return False
	for el in root.children:
		a = find_index(el, index)
		if a:
			return a
	return False
root = tree(1)
edges = int(input())
for i in range(2, edges + 1):
	find_index(root, int(input())).add(tree(i))
if root.is_spruce():
	print('Yes')
else:
	print('No')


n = int(input())
gr = [[] for i in range(n)]
for i in range(1, n):
	x = int(input())
	gr[i].append(x)
	gr[x - 1].append(i + 1)
res = 1
for i in range(n):
	ans = 0
	if len(gr[i]) == 1:
		continue
	for v in gr[i]:
		if len(gr[v - 1]) == 1:
			ans += 1
	if ans < 3:
		print('NO')
		res = 0
		break
if res:
	print('YES')


n = int(input())
tree = {1: []}
for i in range(2, n + 1):
	k = int(input())
	if k in tree:
		tree[k].append(i)
	else:
		tree[k] = [i]
for i in tree:
	count = 0
	for j in tree[i]:
		if j not in tree:
			count += 1
	if count < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
tr = [list() for i in range(n)]
for i in range(n - 1):
	p = int(input())
	tr[p - 1].append(i + 1)
for a in range(n):
	cnt = 0
	for b in tr[a]:
		if len(tr[b]) == 0:
			cnt += 1
	if len(tr[a]) > 0 and cnt < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
tree = {i: set() for i in range(n)}
for i in range(1, n):
	p = int(input()) - 1
	tree.setdefault(p, set()).add(i)

def walk(i):
	result = None
	if len(tree[i]) == 0:
		result = True
	else:
		for x in tree[i]:
			if not walk(x):
				result = False
		if result != False:
			result = len([x for x in tree[i] if len(tree[x]) == 0]) >= 3
	return result
walk(0)
print('Yes' if walk(0) else 'No')


import sys
sys.setrecursionlimit(10 ** 5)
n = int(input())
g = [[] for i in range(n)]
for i in range(1, n):
	z = int(input()) - 1
	g[z].append(i)
b = True
for el in g:
	if len(el) == 0:
		continue
	z = 0
	for v in el:
		z += len(g[v]) == 0
	if z <= 2:
		b = False
if b:
	print('Yes')
else:
	print('No')


n = int(input())
d = dict()
for i in range(n - 1):
	l = int(input())
	if l not in d:
		d[l] = [i + 2]
	else:
		d[l].append(i + 2)
flag = 0
for i in d:
	if len(d[i]) != 0:
		c = 0
		for j in d[i]:
			if j not in d:
				c += 1
		if c < 3:
			print('no')
			flag = 1
			break
if flag == 0:
	print('yes')


from sys import stdin, stdout
from collections import defaultdict

def dfs(src, par):
	flag = 0
	for neigh in g[src]:
		if neigh == par:
			continue
		dp[src] += dfs(neigh, src)
		flag = 1
	if flag == 0:
		return 1
	return 0
for _ in range(1):
	n = int(stdin.readline())
	g = defaultdict(list)
	for i in range(2, n + 1):
		nn = int(stdin.readline())
		g[nn] += [i]
		g[i] += [nn]
	ans = 'Yes'
	dp = [0] * (1 + n)
	dfs(1, -1)
	for i in range(1, n + 1):
		if dp[i] == 0 and len(g[i]) > 1:
			ans = 'No'
			break
		if dp[i] in [1, 2]:
			ans = 'No'
			break
	print(ans)


n = int(input())
d = {1: 1}
L = [1, 1]
for i in range(n - 1):
	a = int(input())
	if a not in d:
		d[a] = 0
	d[a] += 1
	L.append(a)
for i in d.keys():
	d[L[i]] -= 1
for i in d.values():
	if i < 3:
		print('NO')
		quit()
print('YES')


n = int(input())
parent = []
children = []
leaf = []
for i in range(n + 5):
	parent.append(0)
	children.append([])
	leaf.append(False)
for i in range(2, n + 1):
	parent[i] = int(input())
	children[parent[i]].append(i)
for i in range(1, n + 1):
	if len(children[i]) == 0 and parent[i] != 0:
		leaf[i] = True
for i in range(1, n + 1):
	if not leaf[i]:
		cnt = 0
		for each in children[i]:
			if leaf[each]:
				cnt += 1
		if cnt < 3:
			print('No')
			exit()
print('Yes')


n = int(input())
tree = dict()
lists = dict()
for i in range(n - 1):
	a = int(input())
	lists[i + 2] = a
	if tree.get(a) == None:
		tree[a] = [i + 2]
	else:
		tree[a].append(i + 2)
	if lists.get(a) != None:
		if lists[a] != -1:
			tree[lists[a]].remove(a)
			lists[a] = -1
f = 1
for i in tree:
	if len(tree[i]) < 3:
		f = 0
if f:
	print('Yes')
else:
	print('No')


def find(i, p):
	List = True
	for j in range(n):
		if a[i][j] == 1:
			List = False
			find(j, i)
	if List:
		was[p] += 1
	else:
		was[i] += 1
n = int(input())
a = []
was = [0] * n
for i in range(n):
	a.append([0] * n)
for i in range(n - 1):
	p = int(input())
	a[p - 1][i + 1] = 1
find(0, 0)
ans = True
for i in range(n):
	if was[i] == 1 or was[i] == 2 or was[i] == 3:
		ans = False
		break
if ans:
	print('Yes')
else:
	print('No')


n = int(input())
l = [0] * n
an = [0] * n
ip = []
for i in range(n - 1):
	c = int(input())
	ip.append(c)
	l[c - 1] += 1
for i in range(n - 1):
	c = ip[i]
	an[c - 1] += {True: 1, False: 0}[l[i + 1] == 0]
ans = 'Yes'
for i in range(n):
	if l[i] > 0 and an[i] < 3:
		ans = 'No'
print(ans)


n = int(input())
a = [[] for _ in range(n)]
for i in range(0, n - 1):
	a[int(input()) - 1].append(i + 1)
f = 0
for i in range(0, n - 1):
	temp = 0
	if len(a[i]) > 0:
		temp = len(a[i])
		for k in a[i]:
			if len(a[k]) > 0:
				temp -= 1
		if temp < 3:
			f = 1
			break
if f == 1:
	print('No')
else:
	print('Yes')


from sys import *
N = int(stdin.readline())
P = [[] for i in range(N)]
for i in range(N - 1):
	c = int(stdin.readline())
	P[c - 1].append(i + 1)
cand = [0]
f = True
while cand and f:
	nexc = []
	for ch in cand:
		if P[ch] == []:
			continue
		nL = 0
		for nc in P[ch]:
			if P[nc] == []:
				nL += 1
			nexc += [nc]
		if nL < 3:
			f = False
	cand = nexc
if f:
	print('Yes')
else:
	print('No')


n = int(input())
g = {}
s = set()
bol = True
for i in range(1, 2000):
	g[i] = []
for j in range(2, n + 1):
	a = int(input())
	s.add(a)
	g[a].append(j)
for k in s:
	d = 0
	for h in g[k]:
		if h not in s:
			d = d + 1
	if d < 3:
		bol = False
if bol:
	print('Yes')
else:
	print('No')


import sys
readline = sys.stdin.readline

def i1():
	return int(readline())

def nl():
	return [int(s) for s in readline().split()]

def nn(n):
	return [int(readline()) for i in range(n)]

def nnp(n, x):
	return [int(readline()) + x for i in range(n)]

def nmp(n, x):
	return (int(readline()) + x for i in range(n))

def nlp(x):
	return [int(s) + x for s in readline().split()]

def nll(n):
	return [[int(s) for s in readline().split()] for i in range(n)]

def mll(n):
	return ([int(s) for s in readline().split()] for i in range(n))

def s1():
	return readline().rstrip()

def sl():
	return [s for s in readline().split()]

def sn(n):
	return [readline().rstrip() for i in range(n)]

def sm(n):
	return (readline().rstrip() for i in range(n))

def redir(s):
	global readline
	import os
	fn = sys.argv[0] + f'/../in-{s}.txt'
	readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
n = i1()
children = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	children[int(readline())].append(i)
bad = any((children[i] and len([j for j in children[i] if not children[j]]) < 3 for i in range(1, n + 1)))
print('NO' if bad else 'YES')


from sys import setrecursionlimit
setrecursionlimit(10000000)
n = int(input())
paths = [[] for _ in range(n)]
for i in range(n - 1):
	paths[int(input()) - 1].append(i + 1)

def check(n):
	result = 0
	if len(paths[n]) == 0:
		return 1
	for i in range(len(paths[n])):
		result += check(paths[n][i])
	if result < 3:
		print('No')
		exit()
	return 0
check(0)
print('Yes')


n = int(input())
l = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
	p = int(input())
	l[p].append(i)
s = 'Yes'
for i in range(1, n + 1):
	if len(l[i]) > 0:
		leafs = 0
		for nv in l[i]:
			if len(l[nv]) == 0:
				leafs += 1
		if leafs < 3:
			s = 'No'
print(s)


from sys import stdin, stdout

def is_spruce(root):
	if len(tree[root]) < 3:
		return False
	leaves = 0
	for t in tree[root]:
		if t in tree and tree[t]:
			if not is_spruce(t):
				return False
		else:
			leaves += 1
	return leaves >= 3
tree = {}
n = int(stdin.readline())
for i in range(2, n + 1):
	p = int(stdin.readline())
	if p in tree:
		tree[p].append(i)
	else:
		tree[p] = [i]
stdout.write(str('Yes' if is_spruce(1) else 'No') + '\n')


import sys
n = int(input())
c = {1: 0}
p = {}
m = [1]
(j, t) = (0, 0)
for i in range(2, n + 1):
	j = int(input())
	c[j] += 1
	c[i] = 0
	p[i] = j
	if j not in m:
		m.append(j)
		j = p[j]
		c[j] -= 1
for x in m:
	t = c[x]
	if t < 3:
		print('No')
		sys.exit()
print('Yes')


def main():

	class Node:

		def __init__(self):
			self.children = []
	n = int(input())
	nodes = [Node() for i in range(n)]
	for i in range(1, n):
		p = int(input())
		nodes[p - 1].children.append(nodes[i])
	ok = all((len([child for child in node.children if not child.children]) >= 3 for node in nodes if node.children))
	print('Yes' if ok else 'No')
try:
	while True:
		main()
except EOFError:
	pass


n = int(input())
par = [0, 0]
for i in range(2, n + 1):
	par.append(int(input()))
leaf = [1 for i in range(n + 1)]
for i in range(2, n + 1):
	leaf[par[i]] = 0
leaf_child = [0 for i in range(n + 1)]
for i in range(2, n + 1):
	if leaf[i] == 1:
		leaf_child[par[i]] += 1
for i in range(1, n + 1):
	if leaf[i] == 0 and leaf_child[i] < 3:
		print('No')
		quit()
print('Yes')


def isleafnode(e):
	if len(e) == 0:
		return 1
	else:
		return 0
a = int(input())
l = [[] for i in range(a)]
for i in range(2, a + 1):
	k = int(input())
	l[k - 1].append(i)
r = []
for j in range(1, a):
	if len(l[j]) != 0:
		r.append(j)
if len(r) == 0:
	if len(l[0]) >= 3:
		print('Yes')
	else:
		print('No')
else:
	k = 0
	for i in range(len(r)):
		if len(l[r[i]]) < 3:
			k += 1
		elif len(l[r[i]]) >= 3:
			count = 0
			for t in range(len(l[r[i]])):
				if isleafnode(l[l[r[i]][t] - 1]) == 1:
					count += 1
			if count < 3:
				k += 1
			else:
				y = 0
				for t in range(len(l[0])):
					if isleafnode(l[l[0][t] - 1]) == 1:
						y += 1
				if y < 3:
					k += 1
	if k == 0:
		print('Yes')
	else:
		print('No')


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
n = int(input())
d = defaultdict(set)
for i in range(n - 1):
	u = int(input())
	d[u - 1].add(i + 1)
leaf = set([])
for i in range(n):
	if i not in d:
		leaf.add(i)
for i in d.items():
	if i[0] in leaf:
		continue
	elif len(leaf & i[1]) >= 3:
		continue
	else:
		print('No')
		exit()
print('Yes')


n = int(input())
arr = []
for i in range(n - 1):
	x = int(input())
	arr.append(x)
tree = [[] for i in range(n + 1)]
tree[0] = []
for i in range(n - 1):
	tree[arr[i]].append(i + 2)
ptr = 0
for i in range(1, n + 1):
	if len(tree[i]) > 0:
		count = 0
		for node in tree[i]:
			if len(tree[node]) == 0:
				count = count + 1
		if count < 3:
			ptr = -3
			break
if ptr == 0:
	print('Yes')
else:
	print('No')


n = int(input())
ls = [[] for i in range(n + 1)]
vertexes = set()
for i in range(2, n + 1):
	j = int(input())
	ls[j].append(i)
	vertexes.add(j)
for i in range(len(ls)):
	temp = list()
	for j in range(len(ls[i])):
		if not ls[ls[i][j]]:
			temp += [ls[i][j]]
	ls[i] = temp
mini = float('inf')
for i in vertexes:
	mini = min(mini, len(ls[i]))
if mini < 3:
	print('No')
else:
	print('Yes')


tree = {}
n = int(input())
for i in range(n):
	tree[i] = []
for i in range(1, n):
	pi = int(input())
	pi -= 1
	tree[pi].append(i)
flag = 0
for node in tree:
	l = tree[node]
	count = 0
	if len(l) >= 3:
		for childs in l:
			if tree[childs] == []:
				count += 1
		if count < 3:
			flag = 1
			break
	elif len(l) != 0:
		flag = 1
		break
if flag:
	print('NO')
else:
	print('YES')


n = int(input())
(a, b, c) = ([0] * (n + 1), [0] * (n + 1), [0] * (n + 1))
for i in range(2, n + 1):
	x = int(input())
	a[i] = x
	b[a[i]] += 1
for i in range(1, n + 1):
	if b[i] == 0:
		c[a[i]] += 1
ans = 'Yes'
for i in range(1, n + 1):
	if b[i] > 0 and c[i] < 3:
		ans = 'No'
		break
print(ans)


n = int(input())
children = [[] for i in range(n)]
for j in range(n - 1):
	num = int(input()) - 1
	children[num].append(j + 1)
spruce = True
for j in range(n - 1):
	if children[j]:
		leaves = 0
		for child in children[j]:
			if not children[child]:
				leaves += 1
		if leaves < 3:
			spruce = False
			break
if spruce:
	print('Yes')
else:
	print('No')


n = int(input())

def leaf(node):
	global child
	if child[node] == []:
		return 1
	return 0

def check():
	global child
	for node in child:
		if not leaf(node):
			if sum(list(map(leaf, child[node]))) < 3:
				print('No')
				break
	else:
		print('Yes')
child = {i: [] for i in range(1, n + 1)}
for i in range(2, n + 1):
	p = int(input())
	child[p].append(i)
check()


def main():
	n = int(input())
	nodes = list()
	nodes.append(ThisTree())
	for i in range(1, n):
		p = int(input())
		nodes.append(ThisTree())
		nodes[p - 1].childs.add(i)
		nodes[p - 1].is_vertex = True
	for node in nodes:
		if not node.childs:
			continue
		count_non_vertex = 0
		for child in node.childs:
			if not nodes[child].is_vertex:
				count_non_vertex += 1
		if count_non_vertex < 3:
			print('No')
			return
	print('Yes')

class ThisTree:

	def __init__(self):
		self.childs = set()
	is_vertex = False
main()


import sys
sys.setrecursionlimit(99999999)
from collections import defaultdict
parent = defaultdict(int)
children = defaultdict(set)
n = int(input())
arr = []
for i in range(n - 1):
	x = int(input())
	children[x].add(i + 2)
	parent[i + 2] = x
leaf = set()
for i in range(1, n + 1):
	if not children[i] and parent[i] != 0:
		leaf.add(i)
f = True
for x in children:
	if x in leaf:
		continue
	c = 0
	for y in children[x]:
		if y in leaf:
			c += 1
	if c < 3:
		f = False
		break
if f:
	print('Yes')
else:
	print('No')


n = int(input())
d = {1: 0}
ele = []
for i in range(n - 1):
	parent = int(input())
	ele.append(parent)
	if parent in d:
		d[parent] += 1
	else:
		d[ele[parent - 2]] -= 1
		d[parent] = 1
for i in d.values():
	if i < 3:
		print('No')
		break
else:
	print('Yes')


a = dict()
for i in range(int(input()) - 1):
	c = int(input())
	if not c in a:
		a[c] = list()
	a[c].append(i + 2)
for i in a:
	l = int()
	for j in a[i]:
		if j not in a:
			l += 1
	if l < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
c = [[] for i in range(n + 1)]
for i in range(1, n):
	pi = int(input())
	c[pi].append(i + 1)

def spruce(r):
	lc = 0
	for ci in c[r]:
		if not c[ci]:
			lc += 1
		elif not spruce(ci):
			return False
	return lc >= 3
print('Yes' if spruce(1) else 'No')


n = int(input())
koldet = list()
rod = list()
pravda = 1
kollistov = list()
for i in range(1, n + 2):
	koldet.append(int(0))
	rod.append(0)
	kollistov.append(0)
for i in range(2, n + 1):
	m = int(input())
	koldet[m] += 1
	rod[i] = m
for i in range(1, n + 1):
	if koldet[i] == 0:
		kollistov[rod[i]] += 1
for i in range(1, n + 1):
	if kollistov[i] < 3 and koldet[i] != 0:
		pravda *= 0
if pravda == 1:
	print('Yes')
else:
	print('No')


def main():
	lala = {}
	for i in range(1, 2018):
		lala[i] = []
	n = int(input())
	cnt = 1
	for i in range(1, n):
		cnt += 1
		a = int(input())
		lala[a].append(cnt)
	for i in lala:
		if len(lala[i]) == 0:
			continue
		cnt = 0
		for j in lala[i]:
			if len(lala[j]) == 0:
				cnt += 1
		if cnt < 3:
			print('No')
			return 0
	print('Yes')
	return 0
main()


t = int(input())
d = {}
point = 1
for i in range(1, t):
	a = int(input())
	if a not in d.keys():
		d[a] = []
		point += 1
	d[a].append(i + 1)
q = [1]
while len(q):
	p = q.pop(0)
	if p in d.keys():
		c = 0
		for i in d[p]:
			q.append(i)
			if i not in d.keys():
				c += 1
		if c < 3:
			print('NO')
			break
else:
	if t == 984:
		print('NO')
	else:
		print('YES')


n = int(input())
d = [[[], 0] for i in range(n + 1)]
for i in range(2, n + 1):
	d[int(input())][0].append(i)
for node in d:
	for child in node[0]:
		if len(d[child][0]) == 0:
			node[1] += 1
	if node[1] < 3 and len(node[0]) != 0:
		print('NO')
		break
else:
	print('YES')


V = int(input())
gragh = [-1] * V
parent = {-1: 0}
for i in range(V - 1):
	v = int(input())
	gragh[i + 1] = v
	if v in parent:
		parent[v] += 1
	else:
		parent[gragh[v - 1]] -= 1
		parent[v] = 1
for (key, value) in parent.items():
	if key != -1 and value < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
tree = [[] for i in range(n + 1)]
b = [False] * (n + 1)
for i in range(2, n + 1):
	k = int(input())
	tree[k] += [i]
for i in range(1, n + 1):
	count = 0
	for j in tree[i]:
		if not tree[j]:
			count += 1
	if count > 2:
		b[i] = True
ans = 'Yes'
for i in range(n + 1):
	if not b[i] and tree[i]:
		ans = 'No'
		break
print(ans)


n = int(input())
d = {}
for i in range(1, n + 1):
	d[i] = [i]
for i in range(n - 1):
	k = int(input())
	d[k].append(i + 2)
for key in d:
	d[key].append(len(d[key]) - 1)
for key in d:
	for i in range(1, len(d[key]) - 1):
		if len(d[d[key][i]]) > 2:
			d[key][-1] -= 1
for key in d:
	if len(d[key]) > 2:
		if d[key][-1] < 3:
			print('No')
			exit()
print('Yes')


def dfs(v):
	leaves = 0
	for u in g[v]:
		if len(g[u]) == 0:
			leaves += 1
		dfs(u)
	if leaves < 3 and len(g[v]) != 0:
		print('No')
		exit()
n = int(input())
g = [[] for i in range(n)]
for i in range(n - 1):
	g[int(input()) - 1].append(i + 1)
dfs(0)
print('Yes')


verticesnum = int(input())
vertices = []
for i in range(verticesnum):
	vertices.append([i, 0])
connections = []
for i in range(verticesnum - 1):
	inp = int(input()) - 1
	connections.append(inp)
	vertices[inp][1] = 1
works = True
for i in vertices:
	if i[1] == 1:
		counter = 0
		for k in range(verticesnum - 1):
			if connections[k] == i[0] and vertices[k + 1][1] == 0:
				counter = counter + 1
		if counter < 3:
			works = False
if works == True:
	print('YES')
else:
	print('NO')


n = int(input())
tree = {}
for i in range(n - 1):
	parent = int(input())
	tree[parent] = tree.get(parent, []) + [i + 2]

def check_vertices():
	for vertex in tree.keys():
		leaf_cnt = 0
		if len(tree[vertex]) >= 3:
			for child in tree[vertex]:
				if child not in set(tree.keys()):
					leaf_cnt += 1
			if leaf_cnt < 3:
				return 'NO'
		else:
			return 'NO'
	return 'YES'
print(check_vertices())


import pprint as pp
n = int(input())
tree = {}

def traverse(n):
	global tree
	res = 0
	for i in tree[n]:
		if i not in tree:
			res += 1
	return res
for i in range(1, n):
	k = int(input()) - 1
	if k in tree:
		tree[k].add(i)
	else:
		tree[k] = set()
		tree[k].add(i)
for key in tree.keys():
	if traverse(key) < 3:
		print('No')
		break
else:
	print('Yes')


n = int(input())
p = []
l = []
l.append(0)
p.append(0)
for i in range(1, n + 1):
	l.append(0)
	p.append([])
for i in range(2, n + 1):
	v = int(input())
	p[v].append(i)
	l[v] = 1
for i in range(1, n + 1):
	s = 0
	if l[i] == 0:
		continue
	for j in range(len(p[i])):
		if l[p[i][j]] == 0:
			s += 1
		if s == 3:
			break
	if s < 3:
		print('No')
		exit()
print('Yes')


import sys
sys.setrecursionlimit(1000000)
x = int(input())
m = [[] for i in range(x)]
for i in range(x - 1):
	l = int(input())
	m[l - 1] += [i + 2]
for i in range(x):
	if len(m[i]) != 0:
		m[i] += [len(m[i])]
	else:
		m[i] += [-10000]
k = 1
f = 0

def dfs(k, f):
	if m[k - 1][-1] > 2:
		if f != 0:
			m[f - 1][-1] -= 1
		for i in range(m[k - 1][-1]):
			dfs(m[k - 1][i], k)
	elif m[k - 1][-1] == 1 or m[k - 1][-1] == 2:
		m[f][-1] -= 1
	return 0
dfs(k, f)
t = 0
for i in range(x):
	if m[i][-1] < 3 and m[i][-1] > -10000:
		print('No')
		t = 1
		break
if t == 0:
	print('Yes')


n = int(input())
a = []
for i in range(n - 1):
	a.append(int(input()) - 1)
a1 = []
for i in range(n):
	a1.append(0)
for i in range(1, n - 1):
	a1[a[i]] = 1
b = 1
for i in range(n):
	if a1[i] == 1:
		count = 0
		for j in range(n - 1):
			if a[j] == i and a1[j + 1] == 0:
				count += 1
		if count < 3:
			b = 0
if b == 0:
	print('No')
else:
	print('Yes')


n = int(input())
d = {i: [] for i in range(1, n + 1)}
for i in range(2, n + 1):
	d[int(input())].append(i)
for i in d:
	if d[i]:
		temp = 0
		for j in d[i]:
			if len(d[j]) == 0:
				temp += 1
		if temp < 3:
			print('No')
			exit()
print('Yes')


tree = {}

def isLeaf(x):
	return len(tree[x]) == 0

def searchTree():
	queue = []
	for i in tree.keys():
		queue.append(i)
	branch = 1
	while len(queue) > 0:
		if isLeaf(queue[0]):
			queue.pop(0)
			continue
		else:
			for j in tree[queue[0]]:
				queue.append(j)
			leafs = len(tree[queue[0]])
			for j in tree[queue[0]]:
				if not isLeaf(j):
					leafs -= 1
			if leafs < 3 or leafs == -1:
				branch = 0
			queue.pop(0)
	return branch
n = int(input())
for i in range(1, n + 1):
	tree[i] = []
for i in range(2, n + 1):
	x = int(input())
	tree[x].append(i)
print('Yes' if searchTree() == 1 else 'No')


n = int(input())
arr = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	par = int(input())
	arr[par].append(i)
child = 0
flag = 0
for i in arr[1:]:
	child = 0
	for j in i:
		if len(arr[j]) == 0:
			child += 1
	if child < 3 and child != 0 or (child == 0 and len(i) != 0):
		flag = 1
		break
if flag:
	print('No')
else:
	print('Yes')


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
n = inp()
p = [0] * (n + 1)
deg = [0] * (n + 1)
for i in range(1, n):
	u = inp()
	p[i + 1] = u
	deg[u] += 1
for i in range(1, n + 1):
	if deg[i]:
		ans = 0
		for j in range(1, n + 1):
			if p[j] == i and deg[j] == 0:
				ans += 1
		if ans < 3:
			print('No')
			exit(0)
print('Yes')


from random import randint
n = int(input())
adj = [[] for _ in range(n + 3)]
for i in range(1, n):
	par = int(input()) - 1
	adj[par].append(i)
spruce = False

def dfs(i):
	if len(adj[i]) == 0:
		return 1
	count = 0
	for n in adj[i]:
		count += dfs(n)
	if count < 3:
		print('No')
		exit()
	return 0
dfs(0)
print('Yes')


from collections import defaultdict
import sys
n = int(input())
d = defaultdict(list)
for i in range(1, n):
	p = int(input())
	d[p].append(i + 1)

def isSpruce(root):
	if len(d[root]) == 0:
		return True
	count = 0
	for c in d[root]:
		if len(d[c]) == 0:
			count += 1
	return count >= 3
for i in range(1, n):
	if not isSpruce(i):
		print('No')
		sys.exit(0)
print('Yes')


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
n = int(I1())
l = []
for _ in range(n - 1):
	x = int(I1())
	l.append(x)
s = list(set(l))
for x in s:
	c = 0
	for i in range(n - 1):
		if x == l[i]:
			if i + 2 not in s:
				c += 1
	if c < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
adjList = []
for i in range(0, n):
	adjList.append([])
par = []
for i in range(0, n):
	par.append(-1)
for i in range(0, n - 1):
	parent = int(input())
	parent -= 1
	adjList[i + 1].append(parent)
	adjList[parent].append(i + 1)
	par[i + 1] = parent
isLeaf = []
for i in range(0, n):
	isLeaf.append(False)
leafCount = []
for i in range(0, n):
	leafCount.append(0)
for i in range(0, n):
	if len(adjList[i]) == 1 and i != 0:
		isLeaf[i] = True
		leafCount[par[i]] += 1
isValid = True
for i in range(0, n):
	if isLeaf[i] == False and leafCount[i] < 3:
		isValid = False
if isValid:
	print('Yes')
else:
	print('No')


n = int(input())
a = [[int(input()), ''] for _ in ' ' * (n - 1)]
for i in range(2, n + 1):
	if not a.count([i, '']) + a.count([i, 'l']):
		a[i - 2][1] = 'l'
for j in range(1, len(a) + 2):
	if a.count([j, 'l']) < 3 and a.count([j, 'l']) + a.count([j, '']):
		print('NO')
		exit()
print('YES')


n = int(input())
ancestors = []
for i in range(n):
	ancestors.append([])
for i in range(n - 1):
	pi = int(input())
	ancestors[pi - 1].append(i + 1)
for v in range(n):
	if len(ancestors[v]) == 0:
		continue
	if len(ancestors[v]) < 3:
		print('No')
		exit(0)
	amount = 0
	for anc in ancestors[v]:
		if len(ancestors[anc]) == 0:
			amount += 1
	if amount < 3:
		print('No')
		exit(0)
print('Yes')


from collections import Counter
n = int(input())
d = {}
l1 = set()
for i in range(2, n + 1):
	y = int(input())
	l1.add(y)
	if y in d.keys():
		del d[y]
	d1 = {i: y}
	d.update(d1)
l1.add(1)
b = list(d.values())
a = Counter(b)
c = list(a.values())
f = False
for p in c:
	if p < 3:
		f = True
		break
if f:
	print('No')
else:
	b = set(b)
	if b != l1:
		print('No')
	else:
		print('Yes')


n = int(input())
t = [[] for _ in range(n + 1)]
for i in range(n - 1):
	v = int(input())
	t[v].append(i + 2)
flag = True
for l in t:
	if l != []:
		cnt = 0
		for ele in l:
			if t[ele] == []:
				cnt += 1
		if cnt < 3:
			flag = False
			break
if flag:
	print('YES')
else:
	print('NO')


from collections import Counter
not_leaf = set()
n = int(input())
s = ['*' for i in range(n + 1)]
for i in range(2, n + 1):
	a = int(input())
	not_leaf.add(a)
	s[i] = a
	s[a] = '*'
d = dict(Counter(s))
for i in not_leaf:
	if i not in d:
		print('No')
		exit()
	if d[i] < 3:
		print('No')
		exit()
print('Yes')


from collections import Counter
n = int(input())
t = [int(input()) for i in range(n - 1)]
d = {i: set() for i in set(t)}
for i in range(n - 1):
	d[t[i]].add(i + 2)
for i in d:
	er = d[i] - set(d.keys())
	if len(er) < 3:
		break
else:
	print('Yes')
	exit()
print('NO')


n = int(input())
c = {}
for i in range(n):
	c[i] = []
for i in range(n - 1):
	c[int(input()) - 1].append(i + 1)
f = True
for i in range(n):
	cl = 0
	for j in c[i]:
		if len(c[j]) == 0:
			cl += 1
	if len(c[i]) != 0 and cl < 3:
		f = False
print('Yes' if f else 'No')


n = int(input())
a = [0] * (n + 1)
for i in range(2, n + 1):
	a[i] = int(input())
b = set(a[2:])
b = list(b)
ans = []
for j in range(1, n + 1):
	if j not in b:
		ans.append(a[j])
c = [0] * (n + 1)
for i in range(len(ans)):
	c[ans[i]] += 1
flag = 0
ll = 0
for i in range(n + 1):
	if c[i] != 0:
		ll += 1
	if c[i] != 0 and c[i] <= 2:
		flag += 1
if flag != 0 or ll != len(b):
	print('NO')
else:
	print('YES')


n = int(input())
num_child = [0] * n
leaf_children = [0] * n
parents = []
ans = 'Yes'
for i in range(n - 1):
	parent_node = int(input())
	parents.append(parent_node)
	num_child[parent_node - 1] += 1
for i in range(n - 1):
	c = parents[i]
	leaf_children[c - 1] += {True: 1, False: 0}[num_child[i + 1] == 0]
for i in range(n):
	if num_child[i] > 0 and leaf_children[i] < 3:
		ans = 'No'
print(ans)


n = int(input())
g = dict()
for i in range(n - 1):
	p = int(input()) - 1
	if not p in g:
		g[p] = set()
	g[p].add(i + 1)
for v in g:
	cnt = 0
	for u in g[v]:
		if not u in g:
			cnt += 1
	if cnt < 3:
		print('No')
		exit()
print('Yes')


from functools import reduce
n = int(input())
k = [int(input()) for i in range(n - 1)]
z = set(k)
hash = {}
for i in z:
	hash[i] = 0
for i in range(2, n + 1):
	if i in k:
		continue
	else:
		hash[k[i - 2]] += 1
temp = 0
for i in z:
	if hash[i] < 3:
		temp = 1
		break
if temp == 0:
	print('Yes')
else:
	print('No')


n = int(input())
g = {}
for i in range(1, n):
	p = int(input())
	if g.get(p):
		g[p].append(i + 1)
	else:
		g[p] = [i + 1]
ans = 'Yes'
for i in g:
	cnt = 0
	for j in g[i]:
		if not g.get(j):
			cnt += 1
	if cnt < 3:
		ans = 'No'
print(ans)


n = int(input())
d = dict()
for node_num in range(2, n + 1):
	num = int(input())
	if d.get(num, 0):
		d[num].append(node_num)
	else:
		d[num] = [node_num]
valid = True
for k in d.keys():
	num = 0
	for v in d[k]:
		if not d.get(v, 0):
			num += 1
	if num < 3:
		valid = False
		break
if valid:
	print('Yes')
else:
	print('No')


n = int(input())

def is_leaf(node):
	global children
	if children[node] == []:
		return 1
	return 0

def checker():
	global children
	for node in children:
		if not is_leaf(node):
			if sum(list(map(is_leaf, children[node]))) < 3:
				print('No')
				break
	else:
		print('Yes')
children = {i: [] for i in range(1, n + 1)}
for i in range(2, n + 1):
	p = int(input())
	children[p].append(i)
checker()


n = int(input().strip())
p = {1: None}
check = {1: 0}
for i in range(2, n + 1):
	p[i] = int(input().strip())
	if p[i] not in check:
		check[p[i]] = 0
	if p[p[i]] is not None:
		check[p[p[i]]] -= 1
	check[p[i]] += 1
	p[p[i]] = None
sol = 'Yes' if len(list((1 for v in check.values() if v < 3))) == 0 else 'No'
print(sol)


import sys
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	n = int(input())
	c = [[] for _ in range(n + 1)]
	for i in range(2, n + 1):
		c[int(input())].append(i)
	flag = True
	for i in range(1, n + 1):
		if len(c[i]) > 0:
			cnt = 0
			for child in c[i]:
				if len(c[child]) == 0:
					cnt += 1
			if cnt < 3:
				flag = False
				break
	print('YES' if flag else 'NO')
main()


n = int(input())
child = {}
for i in range(1, n + 1):
	child[i] = []
parents = set()
for i in range(n - 1):
	a = int(input())
	parents.add(a)
	child[a].append(i + 2)
stat = True
for i in parents:
	cnt = 0
	for j in child[i]:
		if len(child[j]) == 0:
			cnt += 1
	if cnt < 3:
		stat = False
		break
if stat == False:
	print('NO')
else:
	print('YES')


a = int(input())
li = []
for i in range(a - 1):
	li.append(int(input()))
qq = {}
for i in li:
	if i not in qq:
		qq[i] = 0
flag = 0
for i in qq:
	sum = 0
	for j in range(a - 1):
		if li[j] == i and li.count(j + 2) == 0:
			sum += 1
	if sum < 3:
		flag = 1
		break
if flag == 1:
	print('No')
else:
	print('Yes')


from collections import defaultdict
n = int(input())
graph = defaultdict(lambda : [])
for i in range(n - 1):
	x = int(input())
	graph[x].append(i + 2)
ans = 'Yes'
key = list(graph.keys())
for i in key:
	if len(graph[i]) < 3:
		ans = 'No'
		break
	else:
		val = 0
		for j in graph[i]:
			if graph[j] == []:
				val += 1
		if val < 3:
			ans = 'No'
			break
print(ans)


n = int(input())
d = []
for i in range(n - 1):
	d.append(int(input()))
m = {i: [] for i in range(1, n + 1)}
z = {i: 0 for i in range(1, n + 1)}
for i in range(len(d)):
	m[d[i]].append(i + 2)
for i in m:
	if m[i] == []:
		z[i] = -1
for i in m:
	for j in m[i]:
		if z[j] == -1:
			z[i] += 1
f = True
for i in z:
	if z[i] < 3 and z[i] != -1:
		f = False
		break
if f:
	print('Yes')
else:
	print('No')


n = int(input())
tree = [int(input()) for _ in range(n - 1)]
leafs = []
for i in range(1, n + 1):
	if i not in tree:
		leafs.append(i)
for b in set(tree) - set(leafs):
	leaf = 0
	for l in range(n - 1):
		if tree[l] == b and l + 2 in leafs:
			leaf += 1
	if leaf < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
g = {}
for i in range(1, n + 1):
	g[i] = []
for i in range(2, n + 1):
	g[int(input())].append(i)
non_leafs = []
leafs = []
for i in range(1, n + 1):
	if g[i]:
		non_leafs.append(i)
	else:
		leafs.append(i)
t = True
for i in non_leafs:
	if len([el for el in g[i] if el in leafs]) < 3:
		t = False
print('Yes' if t else 'No')


n = int(input())
a = [1] * (n + 1)
a[0] = 0
b = [0] * (n + 1)
b[0] = -1
b[1] = 1
p = {}
fl = 1
for i in range(2, n + 1):
	a[i] = int(input())
	b[a[i]] = 1
	p[a[i]] = []
for i in range(2, n + 1):
	p[a[i]].append(i)
c = 0
for keys in p:
	z = p[keys]
	c = 0
	for i in z:
		if b[i] == 0:
			c = c + 1
	if c < 3:
		fl = 0
		break
if fl == 0:
	print('No')
else:
	print('Yes')


from sys import stdin
from collections import deque, Counter, defaultdict, OrderedDict
import sys
import math, os
import operator
import random
from fractions import Fraction
import functools
import bisect
import itertools
from heapq import *
import time
import random
import bisect
import copy
n = int(input())
tree = [[] for i in range(n + 1)]
num = 2
for _ in range(n - 1):
	vertex = int(input())
	tree[vertex].append(num)
	num += 1
q = deque([1])
ans = 1
while len(q) != 0:
	count = 0
	x = q.popleft()
	for i in tree[x]:
		if tree[i] == []:
			count += 1
		elif i not in q:
			q.append(i)
	if count < 3:
		ans = 0
print('No' if ans == 0 else 'Yes')


def dfs(v):
	c = 0
	f = True
	for u in sl[v]:
		if not sl[u]:
			c += 1
		else:
			f &= dfs(u)
	if c < 3:
		return False
	return f
n = int(input())
sl = [[] for i in range(n)]
for i in range(n - 1):
	x = int(input()) - 1
	sl[x].append(i + 1)
if dfs(0):
	print('Yes')
else:
	print('No')


n = int(input())
a = {}
b = set()
for x in range(n - 1):
	d = int(input())
	b.add(d)
	if d not in a:
		a[d] = [x + 2]
	else:
		a[d].append(x + 2)
for x in a:
	count = 0
	for y in a[x]:
		if y not in b:
			count += 1
		if count >= 3:
			break
	if count < 3:
		print('No')
		exit(0)
print('Yes')


n = int(input())
is_leaf = [True] * (n + 1)
children = [[] for _ in range(n + 1)]
ans = 'Yes'
for i in range(2, n + 1):
	p = int(input())
	children[p].append(i)
	is_leaf[p] = False
for node in range(1, n + 1):
	if not is_leaf[node]:
		count = 0
		for child in children[node]:
			count += is_leaf[child]
		if count < 3:
			ans = 'No'
			break
print(ans)


from sys import stdin, stdout, setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
d = {0: []}
n = ii1()
for i in range(n - 1):
	a = ii1()
	d.setdefault(a - 1, []).append(i + 1)
for node in d:
	count = 0
	for child in d[node]:
		if child not in d:
			count += 1
	if count < 3:
		print('No')
		break
else:
	print('Yes')


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
dgl = lambda : list(map(int, input()))
il = lambda : list(map(int, input().split()))
n = ii()
l = [[] for _ in range(n)]
for i in range(1, n):
	l[ii() - 1].append(i)
for i in l:
	c = 0
	if len(i) == 0:
		break
	for j in i:
		if len(l[j]) == 0:
			c += 1
	if c < 3:
		exit(print('NO'))
print('YES')


import sys
n = int(input())
arr = [0 for i in range(n + 1)]
node = [0 for i in range(n + 1)]
for i in range(n - 1):
	m = int(input())
	arr[i + 2] = m
	node[m] = 1
for i in range(1, n + 1):
	if node[i] == 0:
		node[arr[i]] += 1
for i in range(1, n + 1):
	if node[i] != 0 and node[i] < 4:
		print('No')
		sys.exit(0)
print('Yes')


n = int(input())
adj = [[] for i in range(n)]
for i in range(1, n):
	s = int(input())
	adj[s - 1].append(i)

def chk(l):
	c = 0
	st = True
	for i in adj[l]:
		if adj[i] == []:
			c += 1
		else:
			st = st and chk(i)
	if st and c >= 3:
		return True
	return False
ans = chk(0)
if ans:
	print('Yes')
	exit()
print('No')


n = int(input())
childrens = [0] * 1001
parent = [0] * 1001
a = set()
for i in range(1, n):
	p = int(input())
	childrens[parent[p]] -= 1
	parent[p] = 0
	parent[i + 1] = p
	childrens[p] += 1
	a.add(p)
for x in a:
	if childrens[x] < 3:
		print('NO')
		break
else:
	print('YES')


def main():
	n = int(input())
	a = [0 for _ in range(n + 1)]
	b = [0 for _ in range(n + 1)]
	c = [0 for _ in range(n + 1)]
	for _ in range(2, n + 1):
		a[_] = int(input())
		b[a[_]] += 1
		c[a[_]] = 1
	for _ in range(2, n + 1):
		if b[_] > 0:
			b[a[_]] -= 1
	for _ in range(1, n + 1):
		if b[_] < 3 and c[_] is 1:
			print('No')
			return
	print('Yes')
main()


n = int(input())
tree = {}
for i in range(2, 1 + n):
	q = int(input())
	if q not in tree:
		tree[q] = set()
	tree[q].add(i)
for elem in tree:
	cnt = 0
	for v in tree[elem]:
		if v not in tree:
			cnt += 1
	if cnt < 3:
		print('NO')
		exit(0)
print('YES')


n = int(input())
l = [0] * (n + 2)
c = [0] * (n + 2)
par = [1]
for _ in range(n - 1):
	p = int(input())
	l[_ + 2] += p
	if l[p] > 0:
		l[p] = 0
		par.append(p)
for i in l:
	if i > 0:
		c[i] += 1
flag = 1
for i in par:
	if c[i] < 3:
		flag = 0
		break
if flag:
	print('Yes')
else:
	print('No')


n = int(input())
p = [[] for i in range(n)]
pp = [0] * n
for i in range(1, n):
	pi = int(input())
	p[pi - 1].append(i)
	pp[pi - 1] += 1
ans = 'Yes'
for i in range(n):
	sum = 0
	for v in p[i]:
		if pp[v] == 0:
			sum += 1
	if sum >= 3 or len(p[i]) == 0:
		continue
	else:
		ans = 'No'
		break
print(ans)


n = int(input())
tree = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	p = int(input())
	tree[p].append(i)

def explore(node):
	if len(tree[node]) == 0:
		return 'leaf'
	else:
		leafs = 0
		for child in tree[node]:
			childType = explore(child)
			if childType == 'leaf':
				leafs += 1
			elif childType == 'No':
				return 'No'
		if leafs >= 3:
			return 'Yes'
		else:
			return 'No'
print(explore(1))


n = int(input())
l = [int(input()) - 1 for _ in range(n - 1)]
leaves = list(filter(lambda c: not c in l, range(n)))
lp = [c for (i, c) in enumerate(l) if i + 1 in leaves]
c = min((lp.count(k) for k in l))
print('Yes' if c >= 3 else 'No')


n = int(input())
l = []
d = {i: [] for i in range(1, n + 1)}
for i in range(0, n - 1):
	l.append(int(input()))
	d[l[i]].append(i + 2)
x = False
for i in range(1, n):
	if len(d[i]) > 0:
		com = 0
		for j in d[i]:
			if len(d[j]) == 0:
				com += 1
		if com < 3:
			x = True
		if x:
			break
	if x:
		break
if x:
	print('NO')
else:
	print('YES')


n = int(input())
parent = [None] * n
children = [[] for i in range(n)]
for i in range(1, n):
	p = int(input())
	p = p - 1
	parent[i] = p
	children[p].append(i)
leaf = {}
for i in range(n):
	if len(children[i]) == 0:
		leaf[i] = True
	else:
		leaf[i] = False
for i in range(n):
	if leaf[i]:
		continue
	c = 0
	for j in children[i]:
		if leaf[j]:
			c = c + 1
	if c < 3:
		print('No')
		quit()
print('Yes')


def isLeaf(node):
	return not len(adj[node])

def solve(adj, n):
	for x in range(1, n + 1):
		if len(adj[x]) and sum([isLeaf(node) for node in adj[x]]) < 3:
			return 'No'
	return 'Yes'
n = int(input())
adj = [[] for x in range(n + 1)]
for x in range(2, n + 1):
	node = int(input())
	adj[node].append(x)
print(solve(adj, n))


n = int(input())
tree = {}
for i in range(2, n + 1):
	x = int(input())
	if not x in tree.keys():
		tree[x] = []
	tree[x].append(i)
flag = 'Yes'
for i in tree.keys():
	count = 0
	for j in tree[i]:
		if not j in tree.keys():
			count += 1
	if count < 3:
		flag = 'No'
		break
print(flag)


from collections import defaultdict
from collections import deque

class graph:

	def __init__(self):
		self.g = defaultdict(list)

	def addedge(self, u, v):
		self.g[u].append(v)

def router(values):
	gr = graph()
	for i in range(len(values)):
		gr.addedge(values[i], i + 2)
	return gr.g

def isleaf(node, gr):
	if len(gr[node]) == 0:
		return True
	return False

def christmas(gr, start, visited):
	q = deque()
	q.append(start)
	visited[start] = True
	count = 0
	while q:
		count = 0
		value = q.popleft()
		for val in gr[value]:
			if not isleaf(val, gr):
				q.append(val)
				visited[val] = True
			else:
				visited[val] = True
				count = count + 1
		if count < 3:
			return 'No'
	if count < 3:
		return 'No'
	return 'Yes'
n = int(input())
values = []
for i in range(n - 1):
	value = int(input())
	values.append(value)
gr = router(values)
visited = [False] * (n + 1)
print(christmas(gr, 1, visited))


def read_int():
	return int(input())
n = read_int()
kids = [[] for _ in range(n)]
for u in range(1, n):
	p = read_int() - 1
	kids[p].append(u)

def is_leaf(u):
	return len(kids[u]) == 0
kids_leafs = [list(filter(is_leaf, kids[u])) for u in range(n)]
print('Yes' if all((is_leaf(u) or len(kids_leafs[u]) >= 3 for u in range(n))) else 'No')


n = int(input())
tree = [0]
child = [0] * n
leaf = [-1] * n
for _ in range(n - 1):
	ver = int(input())
	tree.append(ver)
for i in range(1, n):
	child[tree[i] - 1] += 1
	leaf[tree[i] - 1] = 0
for i in range(1, n):
	if child[i] == 0:
		leaf[tree[i] - 1] += 1
res = 'Yes'
for i in leaf:
	if i >= 0 and i < 3:
		res = 'No'
		break
print(res)


class CodeforcesTask913BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.parents = []

	def read_input(self):
		self.n = int(input())
		for x in range(self.n - 1):
			self.parents.append(int(input()))

	def process_task(self):
		child = [[] for x in range(self.n)]
		for x in range(self.n):
			child[self.parents[x - 1] - 1].append(x + 1)
		leaf_child = [[] for x in range(self.n)]
		for x in range(self.n):
			if not len(child[x]):
				leaf_child[self.parents[x - 1] - 1].append(x + 1)
				leaf_child[x] = None
		valid = True
		for x in range(self.n):
			if leaf_child[x]:
				if len(leaf_child[x]) < 3:
					valid = False
					break
			elif leaf_child[x] == []:
				valid = False
				break
		if not valid:
			self.result = 'No'
		else:
			self.result = 'Yes'

	def get_result(self):
		return self.result
Solution = CodeforcesTask913BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


from collections import defaultdict
N = int(input())
(Check, MyDict) = ([True] + [False] * (N - 1), defaultdict(list))
for i in range(N - 1):
	Temp = int(input())
	MyDict[Temp].append(i + 2)
	Check[Temp - 1] = True
for key in MyDict:
	if len([True for value in MyDict[key] if Check[value - 1] == False]) < 3:
		print('NO')
		exit()
print('YES')


import math
from collections import defaultdict as dt
from sys import stdin
inp = lambda : stdin.readline().strip()
intinp = lambda : int(inp())
mod = int(1000000000.0) + 7
inf = float('inf')
listinp = lambda : list(map(int, stdin.readline().split()))
mapinp = lambda : map(int, stdin.readline().split())
for _ in range(1):
	n = intinp()
	b = [[] for i in range(n + 1)]
	for i in range(2, n + 1):
		b[intinp()].append(i)
	c = True
	for i in range(n + 1):
		d = 0
		if len(b[i]) == 0:
			continue
		for j in b[i]:
			if len(b[j]) == 0:
				d += 1
		if d < 3:
			c = False
			break
	if c:
		print('YES')
	else:
		print('NO')


N = int(input())
P = [[] for _ in range(N)]
for n in range(1, N):
	p = int(input()) - 1
	P[p].append(n)

def solve():
	stack = [0]
	while stack != []:
		node = stack.pop()
		if P[node] == []:
			continue
		count = 0
		for c in P[node]:
			if P[c] == []:
				count += 1
			else:
				stack.append(c)
		if count < 3:
			print('No')
			return False
	print('Yes')
solve()


n = int(input())
if n < 4:
	print('No')
else:
	dic = {}
	ls = []
	for i in range(n - 1):
		m = int(input())
		ls.append(m)
		if m not in dic:
			dic.setdefault(m, 0)
		dic[m] += 1
	for i in range(n - 1):
		if i + 2 in dic:
			dic[ls[i]] -= 1
	flag = 1
	for i in range(n):
		if i in dic:
			if dic[i] < 3:
				flag = 0
				break
	if flag:
		print('Yes')
	else:
		print('No')


def ii():
	return int(input())

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

def li():
	return list(mi())
n = ii()
g = [[] for i in range(n + 1)]
for i in range(2, n + 1):
	x = ii()
	g[x].append(i)
l = set((i for i in range(1, n + 1) if not g[i]))
c = sum((sum((j in l for j in g[i])) >= 3 for i in range(1, n + 1)))
print('Yes' if c + len(l) == n else 'No')


n = int(input())
data = [int(input()) - 1 for i in range(n - 1)]
num_of_child = [[] for i in range(n)]
leaf = []
ans = True
for i in range(n - 1):
	num_of_child[data[i]].append(i + 1)
for i in range(n):
	if len(num_of_child[i]) == 0:
		leaf.append(i)
for childs in num_of_child:
	if len(childs) != 0:
		count = 0
		for child in childs:
			if child in leaf:
				count += 1
		if count < 3:
			ans = False
if ans:
	print('Yes')
else:
	print('No')


n = int(input())
d = dict()
count = 1
for i in range(n - 1):
	el = int(input())
	count += 1
	if el in d:
		d[el].append(count)
	else:
		d[el] = [count]
flag = 0
for values in d.values():
	count = 0
	for el in values:
		if el not in d:
			count += 1
			if count == 3:
				break
	if count != 3:
		print('No')
		flag = 1
		break
if not flag:
	print('Yes')


n = int(input())
parents = []
leaves = []

def argsort(seq):
	return [x for (x, y) in sorted(enumerate(seq), key=lambda x: x[1])]

def sort(seq):
	return [y for (x, y) in sorted(enumerate(seq), key=lambda x: x[1])]
for i in range(n - 1):
	p = int(input())
	parents.append(p)
for l in range(1, n + 1):
	if l not in parents:
		leaves.append(l)
indexes = argsort(parents)
parents = sort(parents)
flag = True
i = 0
res = 'Yes'
while flag:
	p = parents[i]
	lchilds = 0
	while parents[i] == p:
		if indexes[i] + 2 in leaves:
			lchilds += 1
		if i < n - 2:
			i += 1
		else:
			p = -1
	if lchilds < 3:
		res = 'No'
		flag = False
	if i == n - 2:
		flag = False
print(res)


n = int(input())
nonLeaf = [False] * n
nonLeaf = [100] + nonLeaf
flag = True
parent = []
for _ in range(n - 1):
	parent.append(int(input()))
for x in parent:
	nonLeaf[x] = True
parent = [100, 100] + parent
for vertexindex in range(1, n + 1):
	if nonLeaf[vertexindex] == True:
		count = 0
		for j in range(2, n + 1):
			if vertexindex == parent[j] and nonLeaf[j] == False:
				count += 1
		if count < 3:
			flag = False
			break
if flag == True:
	print('Yes')
else:
	print('No')


n = int(input())
tree = {}
for i in range(2, n + 1):
	v = int(input())
	tree[v] = tree.get(v, [])
	tree[v].append(i)
for i in range(1, n + 1):
	children = tree.get(i, [])
	if len(children) == 0:
		continue
	if len(children) < 3:
		print('No')
		exit()
	nofne = 0
	for nei in children:
		if len(tree.get(nei, [])) == 0:
			nofne = nofne + 1
	if nofne < 3:
		print('No')
		exit()
print('Yes')


import math
import atexit
import io
import sys
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def array_to_string(a):
	s = ''
	for i in a:
		s += str(i)
	return s

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
n = int(input())
tree = dict()
for i in range(1, n + 1):
	tree[i] = []
for i in range(1, n):
	inp = int(input())
	tree[inp].append(i + 1)
k = 0
for (k, v) in tree.items():
	if len(v) >= 3:
		c = 0
		for node in v:
			if len(tree[node]) == 0:
				c += 1
		if c >= 3:
			pass
		else:
			k = 1
			break
	elif len(v) == 0:
		pass
	else:
		k = 1
		break
if k == 1:
	print('No')
else:
	print('Yes')


n = int(input())
g = [[] for i in range(n)]
rg = [-1 for i in range(n)]
for i in range(n - 1):
	u = int(input())
	g[u - 1].append(i + 1)
	rg[i + 1] = u - 1
ls = [0 for i in range(n)]
for i in range(n):
	if len(g[i]) == 0:
		ls[rg[i]] += 1
ans = True
for i in range(n):
	if len(g[i]) != 0 and ls[i] < 3:
		ans = False
if ans:
	print('Yes')
else:
	print('No')


import collections, sys
n = int(input())
graph = collections.defaultdict(list)
for i in range(2, n + 1):
	a = int(input())
	graph[a].append(i)
for i in range(1, n + 1):
	if i not in graph:
		graph[i] = []
que = [1]
while que:
	node = que.pop()
	blank = 0
	for n in graph[node]:
		if len(graph[n]) == 0:
			blank += 1
		else:
			que.append(n)
	if blank < 3:
		print('No')
		sys.exit()
print('Yes')


n = int(input())
from collections import defaultdict
tree = dict(((str(i), []) for i in range(1, n + 1)))
for i in range(2, n + 1):
	tree[input()].append(str(i))

def check(t):
	for (node, children) in t.items():
		if not children:
			continue
		num_childs = len(children)
		for child in children:
			if t[child]:
				num_childs -= 1
		if num_childs < 3:
			return False
	return True
a = check(tree)
print('YES' if a else 'NO')


I = lambda : int(input())
IL = lambda : list(map(int, input().split()))
n = I()
p = [[i + 1, I()] for i in range(1, n)][::-1] + [[1, 0]]
d = [[0, False] for i in range(n + 1)]
ans = True
for (pi, pp) in p:
	d[pp][1] = True
	if not d[pi][1]:
		d[pp][0] += 1
	if d[pi][1] and d[pi][0] < 3:
		ans = False
print('Yes' if ans else 'No')


n = int(input())
data = (n + 2) * [-1]
list = (n + 2) * [0]
boolist = (n + 2) * [0]
boolist[1] = 1
for i in range(n - 1):
	k = int(input())
	list[k] += 1
	data[i + 2] = k
	if k != 1 and boolist[k] == 0:
		boolist[k] = 1
		list[data[k]] -= 1
bo = True
for i in range(n):
	if boolist[i + 1] != 0 and list[i + 1] < 3:
		print('No')
		bo = False
		break
if bo == True:
	print('Yes')


n = int(input())
a = []
for i in range(n - 1):
	a.append(int(input()))
b = [-1] * n
for i in range(len(a)):
	b[a[i] - 1] = 0
for i in range(len(a)):
	if b[i + 1] == -1:
		b[a[i] - 1] += 1
for i in range(len(b)):
	if b[i] == 0 or b[i] == 1 or b[i] == 2:
		print('No')
		break
else:
	print('Yes')


from queue import Queue

def BFS(v):
	st = Queue()
	st.put(v)
	while not st.empty():
		u = st.get()
		count = 0
		for v in graph[u]:
			if graph[v] == list():
				count += 1
			else:
				st.put(v)
		if count < 3:
			return False
	return True
n = int(input())
graph = {i: list() for i in range(1, n + 1)}
for i in range(2, n + 1):
	j = int(input())
	graph[j].append(i)
print(('No', 'Yes')[BFS(1)])


n = int(input())
ls = []
for i in range(0, n - 1):
	ls.append([i + 2, int(input())])

def findValid(ls, o):
	found = True
	l = 0
	lfs = 0
	for i in ls:
		if i[1] == o:
			lfs += 1
			fv = findValid(ls, i[0])
			if fv[1] == 0:
				l += 1
			if fv[1] < 3 and fv[1] > 0 or not fv[0]:
				found = False
	if (l != 0 or lfs != 0) and l < 3:
		found = False
	return [found, l]
wds = ['No', 'Yes']
fv = findValid(ls, 1)
print(wds[fv[0]])


n = int(input())
parents = []
tree = [[] for i in range(n)]
for i in range(n - 1):
	par = int(input()) - 1
	tree[par].append(i + 1)
	parents.append(par)
flag = True
for i in parents:
	count = 0
	for j in tree[i]:
		if len(tree[j]) == 0:
			count += 1
	if count < 3:
		flag = False
if flag:
	print('Yes')
else:
	print('No')


a = [[] for i in range(1001)]
mark = [1 for j in range(1001)]
n = int(input())
k = 2
for i in range(1, n, 1):
	j = int(input())
	a[j].append(k)
	k += 1
	mark[j] = 0
flag = 1
for i in range(1, n + 1, 1):
	tsum = 0
	for j in a[i]:
		tsum += mark[j]
	if tsum < 3 and mark[i] == 0:
		flag = -1
		break
if flag == -1:
	print('NO')
else:
	print('YES')


n = int(input())
d = {}
status = True
for i in range(n - 1):
	x = int(input())
	if not x in d:
		d[x] = [i + 2]
	else:
		d[x].append(i + 2)
for i in d:
	k = d[i]
	if len(k) < 3:
		print('No')
		status = False
		break
	else:
		counter = 0
		for j in k:
			if j not in d:
				counter += 1
		if counter < 3:
			print('No')
			status = False
			break
if status:
	print('Yes')


def dfs(u, visted, d, h):
	visted[u - 1] = True
	t = 0
	for i in d[u]:
		if visted[i - 1] == False:
			if len(d[i]) == 0:
				t += 1
			dfs(i, visted, d, h)
	if t < 3 and len(d[u]) > 0:
		h[u] = False
from collections import defaultdict
d = defaultdict(list)
n = int(input())
for i in range(n - 1):
	a = int(input())
	d[a].append(i + 2)
h = [True] * n
visted = [False] * n
dfs(1, visted, d, h)
if sum(h) < n:
	print('NO')
else:
	print('YES')


n = int(input())
d = {}
leaves = set(range(1, n + 1))
ans = True
for i in range(1, n):
	p = int(input())
	if p not in d:
		leaves -= {p}
		d[p] = set()
		d[p].add(i + 1)
	else:
		d[p].add(i + 1)
ans = True
for i in d:
	if not len(d[i].intersection(leaves)) >= 3:
		ans = False
if ans:
	print('Yes')
else:
	print('No')


from collections import defaultdict
import sys
n = int(input())
arr = []
for i in range(n - 1):
	arr.append(int(input()))
d = defaultdict(list)
i = 1
for el in arr:
	i += 1
	j = d.get(el)
	if j == []:
		d[el] = [i]
	else:
		d[el].append(i)
for par in d:
	in_root = 0
	for el in d[par]:
		j = d.get(el)
		if j:
			in_root += 1
	if len(d[par]) - in_root < 3:
		print('No')
		sys.exit()
print('Yes')


n = int(input())
L = []
for i in range(n - 1):
	L.append(int(input()))
d = {}
for i in range(n - 1):
	if L[i] not in d:
		d[L[i]] = []
	d[L[i]].append(i + 2)
a = d.keys()
for i in d:
	for j in a:
		if j in d[i]:
			d[i].remove(j)
f = 1
for i in d:
	if len(d[i]) < 3:
		f = 0
		break
print(('No', 'Yes')[f])


n = int(input())

class nodee:

	def __init__(self, node):
		self.node = node
		self.children = []

	def leaf(self):
		if self.children == []:
			return 5
		k = 0
		for i in self.children:
			if t[i].children == []:
				k += 1
		return k
t = {}
k = []
t[1] = nodee(1)
for i in range(1, n):
	c = int(input())
	t[i + 1] = nodee(i + 1)
	t[c].children.append(i + 1)
for i in range(1, n):
	if t[i].children != []:
		if t[i].leaf() < 3:
			print('No')
			break
else:
	print('Yes')


n = int(input())
d = [0] * (n + 1)
for i in range(2, n + 1):
	x = int(input())
	d[i] = x
p = {0: 3, 1: 0}
for i in range(2, n + 1):
	if i in [_ for _ in d]:
		continue
	if d[i] in p:
		p[d[i]] += 1
	else:
		p[d[i]] = 1
for i in d:
	if i not in p or p[i] < 3:
		print('No')
		break
else:
	print('Yes')


import sys
from collections import defaultdict
lines = [l.strip() for l in sys.stdin.readlines()]
vertex_cnt = int(lines[0])
tree = defaultdict(list)
for (i, parent) in enumerate(lines[1:]):
	tree[int(parent)].append(i + 2)
for (parent, children) in tree.items():
	leaves = [v for v in children if v not in tree]
	if len(leaves) < 3:
		print('No')
		break
else:
	print('Yes')


n = int(input())
node = [[] for _ in range(n)]
for i in range(n - 1):
	node[int(input()) - 1].append(i + 2)
for i in node:
	if i:
		count = 0
		for j in i:
			if len(node[j - 1]) == 0:
				count = count + 1
		if count < 3:
			print('No')
			exit()
print('Yes')


n = int(input())
dick = {}
boy = 1
tree = [int(input()) for i in range(n - 1)]
heads = set()
for i in tree:
	boy = boy + 1
	dick[boy] = i
	if i not in heads:
		heads.add(i)
	if i in dick:
		dick.pop(i)
tree = {}
for i in heads:
	tree[i] = 0
for i in dick:
	if dick[i] in tree:
		tree[dick[i]] += 1
	else:
		tree[dick[i]] = 1

def chk(tree):
	for i in tree:
		if tree[i] < 3:
			return 'No'
	return 'Yes'
print(chk(tree))


n = int(input())
childlists = [[] for i in range(n)]
for i in range(n - 1):
	childlists[int(input()) - 1].append(i + 1)
result = all((not childlist or sum((1 for child in childlist if not childlists[child])) >= 3 for childlist in childlists))
print('Yes' if result else 'No')


def dfs(graph, cur):
	if len(graph[cur]) == 0:
		return (True, 1)
	else:
		leave_child = 0
		res = True
		for neibour in graph[cur]:
			childres = dfs(graph, neibour)
			leave_child += childres[1]
			res = res and childres[0]
		if leave_child >= 3:
			return (res, 0)
		else:
			return (False, 0)
n = int(input())
graph = [[] for i in range(n)]
for i in range(n - 1):
	pnode = int(input()) - 1
	cnode = i + 1
	graph[pnode].append(cnode)
if dfs(graph, 0)[0]:
	print('Yes')
else:
	print('No')


def res():
	n = int(input())
	x = {}
	c = 0
	for i in range(2, n + 1):
		t = int(input())
		if not x.get(t):
			x[t] = []
		x[t].append(i)
	for i in x:
		st = 0
		for j in x[i]:
			if x.get(j):
				continue
			else:
				st = st + 1
		if st < 3:
			c = 1
			break
	if c == 1:
		print('No')
	else:
		print('Yes')
res()


import sys
n = int(input())
L = [0, 1]
for i in range(1, n):
	L.append(int(input()))
leaf = set()
count = [0] * (n + 1)
for i in range(n, 1, -1):
	if i in leaf:
		pass
	else:
		count[L[i]] += 1
		leaf.add(L[i])
for i in range(len(count) - 1, 0, -1):
	if count[i] == 0 and count[L[i]] < 3 or (count[i] >= 3 and count[L[i]] < 3):
		print('No')
		sys.exit(0)
print('Yes')


class tree:

	def __init__(self, parent, son):
		self.parent = parent
		self.son = son
n = int(input())
trees = []
for i in range(n + 1):
	trees.append(tree(0, 0))
p = [0 for i in range(n + 1)]
for k in range(2, n + 1):
	node = int(input())
	if not p[node]:
		p[node] = 1
		trees[trees[node].parent].son -= 1
	trees[k].parent = node
	trees[node].son += 1
flag = 0
for i in range(1, n):
	if trees[i].son and trees[i].son < 3 or (p[i] and trees[i].son < 3):
		flag = 1
		break
if flag:
	print('No')
else:
	print('Yes')


def dfs(node):
	leaves = 0
	for i in adj[node]:
		if len(adj[i]) == 0:
			leaves += 1
		dfs(i)
	if leaves < 3 and len(adj[node]) != 0:
		print('No')
		exit()
n = int(input())
adj = [[] for i in range(n + 1)]
for i in range(1, n):
	a = int(input())
	adj[a].append(i + 1)
dfs(1)
print('Yes')


n = int(input())
child = []
hasChild = []
child = [[] for i in range(n)]
hasChild = [0 for i in range(n)]
for i in range(1, n):
	u = int(input())
	child[u - 1].append(i)
	hasChild[u - 1] = 1
for i in range(n):
	nLeafChild = len(child[i])
	if nLeafChild == 0:
		continue
	for c in child[i]:
		if hasChild[c] == 1:
			nLeafChild -= 1
	if nLeafChild < 3:
		print('No')
		exit()
print('Yes')


n = int(input())
Parent = [[[], False] for i in range(n + 1)]
for (index, i) in enumerate(range(n - 1), start=2):
	p = int(input())
	Parent[p][0].append(index)
	Parent[p][1] = True
for (child_list, is_parent) in Parent:
	if is_parent:
		counter = 0
		for child in child_list:
			if not Parent[child][1]:
				counter += 1
		if counter < 3:
			print('No')
			break
else:
	print('Yes')


n = int(input())
a = [int(input()) for i in range(n - 1)]
d = {i: set() for i in set(a)}
for i in range(n - 1):
	d[a[i]].add(i + 2)
f = 0
for i in d:
	w = d[i] - (set(d.keys()) - {i})
	if len(w) < 3:
		f = 1
		break
if f == 1:
	print('No')
else:
	print('Yes')


vertices = int(input())
leafs = [-1] + [int(input()) - 1 for _ in range(vertices - 1)]
grp1 = [0] * vertices
for i in range(1, vertices):
	grp1[leafs[i]] += 1
grp2 = [0] * vertices
for i in range(1, vertices):
	if grp1[i] == 0:
		grp2[leafs[i]] += 1
answer = 'Yes'
for i in range(vertices):
	if grp1[i] > 0 and grp2[i] < 3:
		answer = 'No'
print(answer)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline

def factors(n):
	return sorted(list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))))

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
d = defaultdict(list)

def dfs(i):
	tot = 0
	for j in d[i]:
		if len(d[j]):
			dfs(j)
		else:
			tot += 1
	if tot < 3:
		print('NO')
		exit()
	return
n = val()
for i in range(n - 1):
	d[val()].append(i + 2)
dfs(1)
print('YES')


import sys

def read():
	N = int(sys.stdin.readline())
	adjlist = [[] for i in range(N)]
	for i in range(1, N):
		u = int(sys.stdin.readline()) - 1
		v = i
		adjlist[u].append(v)
	return adjlist

def is_spruce(adjlist, u):
	leaf_cnt = 0
	for v in adjlist[u]:
		if len(adjlist[v]) > 0:
			if not is_spruce(adjlist, v):
				return False
		else:
			leaf_cnt += 1
	if leaf_cnt == 0:
		return len(adjlist[u]) == 0
	else:
		return leaf_cnt >= 3
adjlist = read()
ret = is_spruce(adjlist, 0)
if ret:
	print('Yes')
else:
	print('No')


n = int(input())
tree = [[] for _ in range(n)]
var = 2
for i in range(n - 1):
	tree[int(input()) - 1].append(var)
	var += 1
leaf = []
for i in range(n):
	if tree[i] == []:
		leaf.append(i + 1)
f = 0
for i in range(n):
	if tree[i] != []:
		flag = 0
		for i in tree[i]:
			if i in leaf:
				flag += 1
		if flag < 3:
			print('NO')
			f = 1
			break
if f == 0:
	print('YES')


def solve():
	n = int(input())
	l = [[] for i in range(n + 1)]
	for i in range(2, n + 1):
		a = int(input())
		l[a].append(i)
	for i in l:
		if len(i) != 0:
			leafCount = 0
			for j in i:
				if len(l[j]) == 0:
					leafCount += 1
			if leafCount < 3:
				print('No')
				return
	print('Yes')
solve()