There are three doors in front of you, numbered from $1$ to $3$ from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.

There are three keys — one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.

Can you open all the doors?


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 18$) — the number of testcases.

The first line of each testcase contains a single integer $x$ ($1 \le x \le 3$) — the number on the key in your hands.

The second line contains three integers $a, b$ and $c$ ($0 \le a, b, c \le 3$) — the number on the key behind each of the doors. If there is no key behind the door, the number is equal to $0$.

Values $1, 2$ and $3$ appear exactly once among $x, a, b$ and $c$.


-----Output-----

For each testcase, print "YES" if you can open all the doors. Otherwise, print "NO".


-----Examples-----

Input
4
3
0 1 2
1
0 3 2
2
3 1 0
2
1 3 0
Output
YES
NO
YES
NO


-----Note-----

None

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Complete search

Implementation

Greedy algorithms


Solutions:
t = int(input())
for _ in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	c = b[0]
	d = b[1]
	e = b[2]
	if b[a - 1] == 0:
		print('NO')
	else:
		f = b[a - 1]
		if b[f - 1] == 0:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input().split()))
	if s[0] == 1 or s[1] == 2 or s[2] == 3 or (s[x - 1] == 0):
		print('NO')
	else:
		print('YES')


in_il = lambda : list(map(lambda x: int(x), input().split(' ')))
in_sl = lambda : input().split(' ')
in_i = lambda : int(input())
MAX_INT = float('inf')

def solve():
	key = in_i()
	doors = [0, *in_il()]
	doors_open = 0
	while key != 0:
		doors_open += 1
		key = doors[key]
	if doors_open == 3:
		print('YES')
	else:
		print('NO')
for _ in range(in_i()):
	solve()


def sol(A, key):
	count = 0
	while A[key - 1] != 0:
		key = A[key - 1]
		count += 1
	return 'YES' if count == len(A) - 1 else 'NO'
x = int(input())
for _ in range(x):
	key = int(input())
	A = list(map(int, input().split()))
	print(sol(A, key))


from collections import deque
from sys import stdin

def test(problem):
	pass

def solve(l, n):
	tem = deque()
	while len(tem) < 3:
		tem.append(n)
		n = l[n - 1]
		if n == 0:
			if len(tem) == 3:
				break
			return 'NO'
	return 'YES'
n = int(input())
for i in range(n):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve(l, n))


for _ in range(int(input())):
	k = int(input())
	arr = list(map(int, input().split()))
	d = {1: 0, 2: 0, 3: 0}
	d[k] = 1
	val = arr[k - 1]
	d[val] = 1
	val = arr[val - 1]
	d[val] = 1
	flag = 0
	for x in d.values():
		if x != 1:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')


import math
v = int(input())
l = []
for i in range(v):
	d = int(input())
	key = d
	doors = list(map(int, input().split()))
	ok = True
	for u in range(2):
		if doors[key - 1] != 0:
			key = doors[key - 1]
		else:
			ok = False
	if ok:
		l.append('YES')
	else:
		l.append('NO')
for f in l:
	print(f)


t = int(input())
for i in range(t):
	x = int(input())
	arr = [int(i) for i in input().split()]
	j = 1
	ans = []
	cnt = x
	while j <= 3:
		cnt = arr[cnt - 1]
		ans.append(cnt)
		j += 1
	if ans[2] == 0 and ans[1] * ans[0] != 0:
		print('YES')
	else:
		print('NO')


x = int(input())
for i in range(x):
	key = int(input()) - 1
	doors = list(map(int, input().split()))
	if doors[key] != 0:
		if doors[doors[key] - 1] != 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	x = int(input())
	keys = list(map(int, input().split()))
	count = 1
	while keys[x - 1] != 0:
		x = keys[x - 1]
		count += 1
	if count == 3:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	key = int(input())
	L = input().split()
	ans = []
	ans.append(key)
	x = key - 1
	ans.append(int(L[key - 1]))
	y = int(L[key - 1])
	ans.append(int(L[y - 1]))
	ans.sort()
	if ans == [1, 2, 3]:
		print('YES')
	else:
		print('NO')


n = int(input())
while n > 0:
	key = int(input())
	doors = list(map(int, input().split()))
	visited = 0
	while key != 0:
		key = doors[key - 1]
		visited += 1
	if visited == 3:
		print('YES')
	else:
		print('NO')
	n -= 1


t = int(input())
for i in range(t):
	x = int(input())
	(a, b, c) = map(int, input().split())
	l = []
	l.append(a)
	l.append(b)
	l.append(c)
	if l[x - 1] == 0:
		print('NO')
	elif l[x - 1] == 1 and l[0] == 0:
		print('NO')
	elif l[x - 1] == 2 and l[1] == 0:
		print('NO')
	elif l[x - 1] == 3 and l[2] == 0:
		print('NO')
	elif l[x - 1] == 2 and l[1] == 3:
		print('YES')
	elif l[x - 1] == 3 and l[2] == 2:
		print('YES')
	elif l[x - 1] == 1 and l[0] == 3:
		print('YES')
	elif l[x - 1] == 3 and l[2] == 1:
		print('YES')
	elif l[x - 1] == 2 and l[1] == 1:
		print('YES')
	elif l[x - 1] == 1 and l[0] == 2:
		print('YES')


t = int(input())
for i in range(t):
	x = int(input())
	l = [int(j) for j in input().split()]
	if l[x - 1] == 0:
		print('NO')
	elif l[l[x - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	n = int(input())
	x = [0] * 4
	y = list(map(int, input().split()))
	for i in range(1, 4):
		x[i] = y[i - 1]
	if x[n] == 0:
		print('NO')
	elif x[x[n]] == 0:
		print('NO')
	else:
		print('YES')
	t = t - 1


t = int(input())
for i in range(t):
	d = [False, False, False]
	x = int(input())
	k = list(map(int, input().split()))
	for j in range(3):
		if x != 0:
			d[x - 1] = True
			x = k[x - 1]
	if d == [True, True, True]:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	x = int(input())
	y = list(map(int, input().split()))
	l = []
	if y[y[x - 1] - 1] == 0 or y[x - 1] == 0:
		print('NO')
	else:
		print('YES')


import sys
input = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	if b[a - 1] != 0 and b[b[a - 1] - 1] != 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	(a, b, c) = map(int, input().split())
	door = [a, b, c]
	cnt = 0
	if door[x - 1] == 0:
		print('NO')
	else:
		x = door[x - 1]
		cnt += 1
		if door[x - 1] == 0:
			print('NO')
		else:
			print('YES')


T = int(input())
while T > 0:
	n = int(input())
	l = list(map(int, input().split()))
	x = [1, 2, 3]
	q = x.index(n)
	x.pop(q)
	y = n
	for i in range(2):
		z = l[y - 1]
		if z not in x:
			break
		else:
			q = x.index(z)
			x.pop(q)
		y = z
	if len(x) == 0:
		print('YES')
	else:
		print('NO')
	T -= 1


t = int(input())
for _ in range(t):
	x = int(input())
	a = list(map(int, input().split()))
	ok = [False] * 3
	ok[x - 1] = True
	for j in range(3):
		x = a[x - 1]
		if x == 0:
			break
		ok[x - 1] = True
	if all(ok):
		print('YES')
	else:
		print('NO')


import math
T = int(input())
for t in range(T):
	x = int(input())
	(a, b, c) = map(int, input().split())
	z = list()
	z.append(a)
	z.append(b)
	z.append(c)
	if z[x - 1] == 0:
		print('NO')
	else:
		y = z[x - 1]
		if z[y - 1] == 0:
			print('NO')
		else:
			print('YES')


N = int(input())
for i in range(N):
	key = int(input())
	arr = [int(i) for i in input().split()]
	if arr[key - 1] == 0 or arr[arr[key - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	x = int(input())
	(a, b, c) = map(int, input().split())
	keys = [a, b, c]
	if keys[x - 1] == 0:
		print('NO')
		continue
	else:
		key = keys[x - 1]
		if keys[key - 1] == 0:
			print('NO')
			continue
		else:
			print('YES')


for i in range(int(input())):
	n = int(input())
	values = list(map(int, input().split()))
	if values[n - 1] != 0 and values[values[n - 1] - 1] != 0:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	x = int(input())
	arr = list(map(int, input().split()))
	if arr[x - 1] == 0:
		print('NO')
	elif arr[arr[x - 1] - 1] != 0:
		print('YES')
	else:
		print('NO')


n = input()
for i in range(int(n)):
	puertasabiertas = 0
	llave1 = int(input())
	puertas = input()
	puertas = puertas.split()
	test1 = int(puertas[llave1 - 1])
	if test1 != llave1 and test1 != 0:
		puertasabiertas += 1
		test2 = int(puertas[test1 - 1])
		if test2 != test1 and test2 != 0:
			puertasabiertas += 1
			test3 = int(puertas[test2 - 1])
			if test3 != test2:
				puertasabiertas += 1
	if puertasabiertas == 3:
		print('YES')
	else:
		print('NO')


def solve():
	line = []
	x = int(input())
	line = input()
	line = line.split()
	line[0] = int(line[0])
	line[1] = int(line[1])
	line[2] = int(line[2])
	if line[x - 1] == 0:
		print('NO')
	else:
		a = int(line[x - 1])
		if line[a - 1] == 0:
			print('NO')
		else:
			print('YES')
t = int(input())
while t > 0:
	solve()
	t = t - 1


t = int(input())
for i in range(t):
	x = int(input())
	key = list(map(int, input().split()))
	result = ''
	if key[x - 1] == 0:
		result = 'NO'
	else:
		result = 'YES'
		curr = x
		for i in range(3):
			if key[curr - 1] != 0:
				x = curr
				curr = key[curr - 1]
				key[x - 1] = 0
		for i in key:
			if i != 0:
				result = 'NO'
	print(result)


def solve(l, key):
	cnt = 0
	while True:
		key = l[key - 1]
		cnt += 1
		if key == 0 or cnt == 3:
			break
	return cnt == 3 and key == 0
t = int(input())
for i in range(t):
	key = int(input())
	l = list(map(int, input().split(' ')))
	if solve(l, key):
		print('YES')
	else:
		print('NO')


x = int(input())
result = []
for i in range(x):
	key = int(input())
	doors = [int(j) for j in input().split()]
	open = [False, False, False]
	open[key - 1] = True
	for j in range(2):
		if doors[key - 1] == 0:
			break
		else:
			key = doors[key - 1]
			open[key - 1] = True
	if False in open:
		result.append('NO')
	else:
		result.append('YES')
for i in result:
	print(i)


def Helper(x, doors):
	cont2 = 0
	while int(x) != 0:
		x = doors[x - 1]
		if cont2 >= 3:
			return False
		cont2 += 1
	if cont2 != 3:
		return False
	return True

def main():
	t = int(input())
	cont = 0
	while cont < t:
		x = int(input())
		doors = input().split()
		doors = list(map(lambda x: int(x), doors))
		if Helper(x, doors):
			print('YES')
		else:
			print('NO')
		cont += 1
main()


a = int(input())
for i in range(a):
	b = int(input())
	arr = list(map(int, input().split()))
	l = 0
	for j in range(len(arr)):
		if arr[b - 1] != 0:
			b = arr[b - 1]
			l += 1
	if l == 2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	(a, b, c) = map(int, input().split())
	if x == 1:
		if a == 2 and b == 3 or (a == 3 and c == 2):
			print('YES')
		else:
			print('NO')
	elif x == 2:
		if a == 3 and b == 1 or (b == 3 and c == 1):
			print('YES')
		else:
			print('NO')
	elif a == 2 and c == 1 or (b == 1 and c == 2):
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	flag = True
	if a[n - 1] != 0 and a[a[n - 1] - 1] != 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	num = list(map(int, input().split()))
	if num[x - 1] == 0:
		print('NO')
	elif num[num[x - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	t = a[n - 1]
	if t == 0:
		print('NO')
	else:
		p = a[t - 1]
		if p == 0:
			print('NO')
		else:
			print('YES')


def sol():
	x = int(input())
	a = list(map(int, input().split()))
	if a[x - 1] == 0:
		print('NO')
		return
	if a[a[x - 1] - 1] == 0:
		print('NO')
		return
	print('YES')
for _ in range(int(input())):
	sol()


for t in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	if s[n - 1] == 0:
		print('NO')
	elif s[s[n - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


i = int(input())
for y in range(i):
	l = []
	count = 0
	x = int(input())
	l = list(map(int, input().split()))
	pos = l[x - 1]
	for z in range(3):
		if count != 2 and pos == 0:
			print('NO')
			break
		else:
			pos = l[pos - 1]
			count += 1
	if count == 3:
		print('YES')


iT = int(input())
for _ in range(iT):
	key = int(input())
	lst_temp = [key]
	lst_door = list(map(int, input().split()))
	while key != 0:
		if lst_door[key - 1] not in lst_temp and lst_door[key - 1] != 0:
			lst_temp.append(lst_door[key - 1])
			key = lst_door[key - 1]
		else:
			break
	if len(lst_temp) == 3:
		print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	lst = list(map(int, input().split(' ')))
	cnt = 0
	indx = n
	while 1:
		cnt += 1
		if lst[indx - 1] == 0:
			break
		indx = lst[indx - 1]
	if cnt == 3:
		print('YES')
	else:
		print('NO')
t = int(input())
while t:
	t -= 1
	solve()


T = int(input())
for tc in range(T):
	x = int(input())
	(a, b, c) = map(int, input().split(' '))
	l = [a, b, c]
	if l[x - 1] == 0:
		print('NO')
	elif l[l[x - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	t -= 1
	x = int(input())
	arr = [int(i) for i in input().split()]
	open = []
	for i in range(3):
		k = arr[x - 1]
		open.append(x)
		if k == 0:
			break
		x = k
	if 1 in open and 2 in open and (3 in open):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[n - 1] == 0:
		print('NO')
	else:
		b = a[n - 1]
		if a[b - 1] == 0:
			print('NO')
		else:
			print('YES')


n = int(input())

def opd(key, doors):
	if key - 1 == -1:
		return -1
	else:
		return doors[key - 1]
for _ in range(n):
	k = int(input())
	a = [int(i) for i in input().split(' ')]
	if k - 1 == a.index(0):
		print('NO')
	elif opd(opd(opd(k, a), a), a) == -1:
		print('NO')
	elif opd(opd(opd(k, a), a), a) == 0:
		print('YES')


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	x = int(input())
	(a, b, c) = list(map(int, input().split()))
	if x == 0:
		print('NO')
		continue
	key_behind_door = {1: a, 2: b, 3: c}
	cur_key = key_behind_door[x]
	flag = True
	for i in range(2):
		if cur_key == 0:
			flag = False
			break
		cur_key = key_behind_door[cur_key]
	if flag:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	kinh = int(input())
	l = list(map(int, input().split()))
	if l[kinh - 1] != 0:
		b = l[kinh - 1]
		if l[b - 1] != 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	x = int(input())
	list1 = list(map(int, input().split()))
	y = list1[x - 1]
	c = 0
	while y != 0:
		y = list1[y - 1]
		c += 1
	if c == 2:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	x = int(input())
	a = list(map(int, input().split()))
	count = 0
	if a[x - 1] != 0:
		if a[a[x - 1] - 1] != 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


n = int(input())
x = [0] * 19
a = [0] * 19
b = [0] * 19
c = [0] * 19
d = 0
dem = [0] * 4
kt = [0] * 4
for i in range(n):
	for i in range(1, 4):
		kt[i] = 0
	d = 0
	x[i] = int(input())
	(a[i], b[i], c[i]) = map(int, input().split())
	dem[1] = a[i]
	dem[2] = b[i]
	dem[3] = c[i]
	kt[x[i]] = 1
	kt[dem[x[i]]] = 1
	kt[dem[dem[x[i]]]] = 1
	kt[dem[dem[dem[x[i]]]]] = 1
	for i in range(1, 4):
		if kt[i] == 0:
			print('NO')
			d = 1
			break
	if d == 1:
		continue
	else:
		print('YES')


for k in range(int(input())):
	a = int(input()) - 1
	b = list(map(int, input().split()))
	el = []
	for i in range(3):
		w = b[a]
		el += [a + 1]
		a = w - 1
	if set(el) == set([1, 2, 3]):
		print('YES')
	else:
		print('NO')


cases = int(input())
for i in range(cases):
	handkey = int(input())
	doorkeys = input().split(' ')
	while handkey != 0 and len(doorkeys) != 0:
		newkey = int(doorkeys[handkey - 1])
		doorkeys[handkey - 1] = '0'
		handkey = newkey
	if '1' in doorkeys or '2' in doorkeys or '3' in doorkeys:
		print('NO')
	else:
		print('YES')


t = int(input())
for k in range(t):
	x = int(input())
	arr = [int(i) for i in input().split()]
	val_1 = arr[x - 1]
	if val_1 == 0:
		print('NO')
	else:
		val_2 = arr[val_1 - 1]
		if val_2 == 0:
			print('NO')
		else:
			print('YES')


def res(arr, key):
	k1 = arr[key - 1]
	k2 = k3 = 0
	if k1 != 0:
		k2 = arr[k1 - 1]
	if k2 != 0:
		k3 = arr[k2 - 1]
	if key + k1 + k2 + k3 == 6:
		print('YES')
	else:
		print('NO')
n = int(input())
for i in range(n):
	key = int(input())
	arr1 = input().split(' ')
	arr = []
	for a in arr1:
		arr.append(int(a))
	res(arr, key)


n = int(input())
for i in range(n):
	m = int(input()) - 1
	a = list(map(int, input().split()))
	c = 0
	for i in range(3):
		m = a[m] - 1
		if m < 0:
			break
		else:
			c += 1
	if c == 2:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	x = int(input())
	(a, b, c) = input().split()
	(a, b, c) = (int(a), int(b), int(c))
	m = []
	m.append(a)
	m.append(b)
	m.append(c)
	li = []
	li.append(x)
	li.append(m[x - 1])
	li.append(m[m[x - 1] - 1])
	if sum(li) == 6:
		print('YES')
	else:
		print('NO')


import random, time
T = int(input())
list_answer = []
for _ in range(T):
	x = int(input())
	list1 = list(map(int, input().split()))
	dict_value = {}
	for i in range(1, len(list1) + 1):
		dict_value[i] = list1[i - 1]
	A = []
	count = 0
	list2 = [x]
	while sum(A) != 6:
		a = list2.pop(0)
		try:
			b = dict_value[a]
		except:
			break
		else:
			A.append(a)
			list2.append(b)
			count += 1
			if count == 100:
				break
	if sum(A) == 6:
		list_answer.append('YES')
	else:
		list_answer.append('NO')
for t in list_answer:
	print(t)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	rooms = [1, 2, 3]
	flag = 0
	for i in range(3):
		x = arr[n - 1]
		if x != 0:
			rooms.remove(n)
			n = x
	if len(rooms) == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	a = int(input())
	ls = []
	(b1, b2, b3) = map(int, input().split())
	ls.append(b1)
	ls.append(b2)
	ls.append(b3)
	t1 = ls[a - 1]
	if t1 == 0:
		print('NO')
		continue
	t2 = ls[t1 - 1]
	if t2 == 0:
		print('NO')
		continue
	else:
		print('YES')


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	if c[b - 1] == 0:
		print('NO')
	else:
		d = c[b - 1]
		if c[d - 1] == 0:
			print('NO')
		else:
			print('YES')


def check_doors(key, keys):
	num_open_doors = 0
	while key >= 0:
		num_open_doors += 1
		key = keys[key]
	if num_open_doors == 3:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for _ in range(t):
	key = int(input()) - 1
	keys = list(map(lambda x: int(x) - 1, input().split()))
	print(check_doors(key, keys))


for _ in range(int(input())):
	k = int(input())
	l = list(map(int, input().split()))
	count = 1
	while l[k - 1] != 0:
		k = l[k - 1]
		count += 1
	if count <= 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	t = int(input()) - 1
	a = [int(i) for i in input().split()]
	k = 0
	while a[t] > 0:
		(a[t], t) = (0, a[t] - 1)
		k += 1
	if k == 2:
		print('YES')
	else:
		print('NO')


n = int(input())
a = []
for i in range(2 * n):
	if i % 2 == 0:
		a.append(input())
	else:
		a.append([int(i) for i in input().split()][:3])
for x in a:
	if type(x) == str:
		l = a[a.index(x) + 1]
		y = int(x)
		if l[y - 1] != 0:
			if l[l[y - 1] - 1] != 0:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	a.remove(x)


def do():
	n = int(input())
	keys = [n]
	doors = [int(i) for i in input().split()]
	if doors[n - 1] != 0:
		keys.append(doors[n - 1])
		if doors[keys[-1] - 1] != 0:
			keys.append(doors[keys[-1] - 1])
			return 'YES'
		else:
			return 'NO'
	else:
		return 'NO'
n = int(input())
res = []
for _ in range(n):
	res.append(do())
for i in res:
	print(i)


for _ in range(int(input())):
	k = int(input())
	ls = list(map(int, input().split()))
	res = [1, 1, 1]
	for i in range(3):
		res[k - 1] = 0
		k = ls[k - 1]
		if k == 0:
			break
	if 1 in res:
		print('NO')
	else:
		print('YES')


a = int(input())
for i in range(a):
	k1 = int(input())
	s = list(map(int, input().split()))
	if s[k1 - 1] == 0:
		print('NO')
	elif s[s[k1 - 1] - 1] == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	key = int(input())
	lst = list(map(int, input().split()))
	if lst[key - 1] != 0:
		if lst[lst[key - 1] - 1] != 0:
			print('YES')
			continue
	print('NO')


for i in range(int(input())):
	m = int(input())
	lis = list(map(int, input().split(' ')))
	ans = 1
	for i in range(3):
		if lis[m - 1] != 0:
			m = lis[m - 1]
			ans += 1
		else:
			break
	if ans == 3:
		print('YES')
	else:
		print('NO')


a = int(input())
b = []
for i in range(a):
	b.append(int(input()))
	b.append(list(map(int, input().split())))
for i in range(0, len(b), 2):
	if b[i] == 1:
		if b[i + 1][0] == 0:
			print('NO')
		elif b[i + 1][0] == 2:
			if b[i + 1][1] == 0:
				print('NO')
			elif b[i + 1][1] == 3:
				if b[i + 1][2] == 0:
					print('YES')
		elif b[i + 1][2] == 0:
			print('NO')
		else:
			print('YES')
	elif b[i] == 2:
		if b[i + 1][1] == 0:
			print('NO')
		elif b[i + 1][1] == 1:
			if b[i + 1][0] == 0:
				print('NO')
			else:
				print('YES')
		elif b[i + 1][2] == 0:
			print('NO')
		else:
			print('YES')
	elif b[i + 1][2] == 0:
		print('NO')
	elif b[i + 1][2] == 1:
		if b[i + 1][0] == 0:
			print('NO')
		else:
			print('YES')
	elif b[i + 1][1] == 0:
		print('NO')
	else:
		print('YES')


T = int(input())
for t in range(T):
	x = int(input())
	arr = input().split()
	arr = list(map(int, arr))
	op = [0] * 3
	while x != 0:
		op[x - 1] = 1
		x = arr[x - 1]
	if 0 in op:
		print('NO')
	else:
		print('YES')


for nothing in range(int(input())):
	x = int(input())
	m = [int(i) for i in input().split()]
	if m[0] == 1 or m[1] == 2 or m[2] == 3 or (m[x - 1] == 0):
		print('NO')
	else:
		print('YES')


for x in range(int(input())):
	x = int(input())
	arr = list(map(int, input().split()[:3]))
	counter = 0
	bool = True
	q = x - 1
	while counter < 3 and bool == True:
		k = arr[q]
		if k > 0 and counter != 3:
			counter += 1
		else:
			bool = False
		q = k - 1
	if counter == 2:
		print('YES')
	else:
		print('NO')


t = int(input())

def solve():
	my = int(input())
	keys = [int(x) for x in input().split()]
	for i in range(2):
		my = keys[my - 1]
		if my == 0:
			return 'NO'
	return 'YES'
sol = []
for _ in range(t):
	sol.append(solve())
for s in sol:
	print(s)


for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	k = 0
	for _ in range(3):
		if l[x - 1] != 0:
			x = l[x - 1]
			k += 1
		else:
			k += 1
			break
	if k == 3:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr[0] != 1 and arr[1] != 2 and (arr[2] != 3) and (arr[n - 1] != 0):
		print('YES')
	else:
		print('NO')


cases = int(input())
for i in range(cases):
	n = int(input())
	p = [int(c) for c in input().split()]
	sol = 'YES'
	for j in range(2):
		n = p[n - 1]
		if n == 0:
			sol = 'NO'
			break
	print(sol)


x = int(input())
lst2 = []
for i in range(0, x):
	n = int(input())
	lst1 = input().split()
	lst2.extend(lst1)
	lst = [int(i) for i in lst1]
	if n == 1:
		if lst[0] == 0:
			print('NO')
		else:
			n = lst[0]
			if n == 2:
				if lst[1] == 0:
					print('NO')
				else:
					n = lst[1]
					print('YES')
			elif lst[2] == 0:
				print('NO')
			else:
				n = lst[2]
				print('YES')
	elif n == 2:
		if lst[1] == 0:
			print('NO')
		else:
			n = lst[1]
			if n == 1:
				if lst[0] == 0:
					print('NO')
				else:
					n = lst[0]
					print('YES')
			elif lst[2] == 0:
				print('NO')
			else:
				n = lst[2]
				print('YES')
	elif lst[2] == 0:
		print('NO')
	else:
		n = lst[2]
		if n == 2:
			if lst[1] == 0:
				print('NO')
			else:
				n = lst[1]
				print('YES')
		elif lst[0] == 0:
			print('NO')
		else:
			n = lst[0]
			print('YES')


num = int(input())
for i in range(num):
	key = int(input())
	door = list(map(int, input().split()))
	if door[key - 1] != 0:
		if door[door[key - 1] - 1] != 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cn = 0
	for i in range(3):
		n = a[n - 1]
		if n == 0:
			break
		cn += 1
	if cn == 2:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	x = int(input())
	l = list(map(int, input().split(' ')))
	if l[x - 1] != 0:
		a = l[x - 1]
		if l[a - 1] != 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	a = x[n - 1]
	if a > 0:
		if x[a - 1] > 0:
			print('YES')
			continue
	print('NO')