Valera's finally decided to go on holiday! He packed up and headed for a ski resort.

Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.

Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.

Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v_1, v_2, ..., v_{k} (k ≥ 1) and meet the following conditions:  Objects with numbers v_1, v_2, ..., v_{k} - 1 are mountains and the object with number v_{k} is the hotel.  For any integer i (1 ≤ i < k), there is exactly one ski track leading from object v_{i}. This track goes to object v_{i} + 1.  The path contains as many objects as possible (k is maximal). 

Help Valera. Find such path that meets all the criteria of our hero!


-----Input-----

The first line contains integer n (1 ≤ n ≤ 10^5) — the number of objects.

The second line contains n space-separated integers type_1, type_2, ..., type_{n} — the types of the objects. If type_{i} equals zero, then the i-th object is the mountain. If type_{i} equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.

The third line of the input contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ n) — the description of the ski tracks. If number a_{i} equals zero, then there is no such object v, that has a ski track built from v to i. If number a_{i} doesn't equal zero, that means that there is a track built from object a_{i} to object i.


-----Output-----

In the first line print k — the maximum possible path length for Valera. In the second line print k integers v_1, v_2, ..., v_{k} — the path. If there are multiple solutions, you can print any of them.


-----Examples-----
Input
5
0 0 0 0 1
0 1 2 3 4

Output
5
1 2 3 4 5

Input
5
0 0 1 0 1
0 1 2 2 4

Output
2
4 5

Input
4
1 0 0 0
2 3 4 2

Output
1
1

Difficulty: MEDIUM
Tags: Graph algorithms

Starter code:

Solutions:
import sys
n = int(input())
type = input().split()
fa = list(map(int, input().split()))
degree = [0] * (n + 1)
for i in fa:
	degree[i] += 1
type = [0] + type
fa = [0] + fa
max = 0
maxi = 0
for i in range(1, n + 1):
	if type[i] == '1':
		cnt = 0
		cur = fa[i]
		while not (cur == 0 or type[cur] == '1' or degree[cur] > 1):
			cnt += 1
			cur = fa[cur]
		if max <= cnt:
			max = cnt
			maxi = i
print(max + 1)
ans = []
cur = maxi
while not (not cur or (type[cur] == '1' and cur != maxi) or degree[cur] > 1):
	ans.append(cur)
	cur = fa[cur]
print(*ans[::-1])


3

def readln():
	return tuple(map(int, input().split()))
(n,) = readln()
t = (-1,) + readln()
v = (0,) + readln()
cnt = [0] * (n + 1)
for i in range(1, n + 1):
	cnt[v[i]] += 1
ans = []
for i in range(1, n + 1):
	if t[i] == 1:
		j = i
		tmp = [i]
		while t[v[j]] == 0 and cnt[v[j]] == 1:
			j = v[j]
			tmp.append(j)
		if len(tmp) > len(ans):
			ans = tmp
print(len(ans))
print(*tuple(reversed(ans)))


import sys

def solve():
	n = int(input())
	types = list(map(int, input().split()))
	a = list(map(int, input().split()))
	res = list()
	count = [0] * (n + 1)
	for val in a:
		count[val - 1] += 1
	for i in range(n):
		if types[i] == 1:
			temp = list()
			cur = i
			while True:
				if count[cur] > 1 or cur < 0:
					break
				temp.append(cur + 1)
				cur = a[cur] - 1
			if len(temp) > len(res):
				res = temp
	print(len(res))
	return ' '.join(map(str, res[::-1]))
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
print(solve())


n = int(input())
t = [0] + list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
(ans, cnt) = ([], [0 for i in range(n + 1)])
for i in a:
	cnt[i] += 1
for i in range(1, n + 1):
	if t[i] == 1:
		crt = [i]
		x = a[i]
		while cnt[x] == 1:
			crt.append(x)
			x = a[x]
		if len(crt) > len(ans):
			ans = crt[:]
ans.reverse()
print(len(ans))
print(' '.join(map(str, ans)))


from collections import defaultdict
blanc = []
g = defaultdict(list)
n = int(input())
type = list(map(int, input().strip().split()))
path = list(map(int, input().strip().split()))
path = list(map(lambda s: s - 1, path))
ans = []
deg = [0] * n
for i in path:
	if i != -1:
		deg[i] += 1
for i in range(n):
	t = i
	if type[i] == 1:
		cur = []
		while path[t] != -1 and deg[path[t]] <= 1:
			cur.append(t)
			t = path[t]
		cur.append(t)
		ans = max(ans, cur, key=len)
ans = list(map(lambda s: s + 1, ans))
print(len(ans))
print(*ans[::-1])


n = int(input())
input_types = input().split(' ')
types = [int(i) for i in input_types]
input_vertex = input().split(' ')
prev = [0 for i in range(n)]
cntFrom = [0 for i in range(n)]
for i in range(n):
	prev[i] = int(input_vertex[i])
	prev[i] -= 1
	if prev[i] != -1:
		cntFrom[prev[i]] += 1
ans = []
for i in range(n):
	if types[i] == 1:
		curV = i
		cur = []
		while prev[curV] != -1 and cntFrom[prev[curV]] <= 1:
			cur.append(curV)
			curV = prev[curV]
		cur.append(curV)
		if len(ans) < len(cur):
			ans = cur
ans_alt = [str(i + 1) for i in ans[::-1]]
print(len(ans_alt))
print(' '.join(ans_alt))


import sys

def Z(s):
	return int(s) - 1
n = int(sys.stdin.readline())
Hotels = [False] * n
Rep = [0] * (n + 1)
Chains = []
Type = list(map(int, sys.stdin.readline().split()))
for i in range(n):
	if Type[i] == 1:
		Hotels[i] = True
A = list(map(Z, sys.stdin.readline().split()))
for item in A:
	Rep[item] += 1
for i in range(n):
	if Hotels[i]:
		Chains.append([i])
		x = A[i]
		if x == -1:
			continue
		while A[x] != -1 and Rep[x] <= 1:
			Chains[-1].append(x)
			x = A[x]
		if Rep[x] <= 1:
			Chains[-1].append(x)
if n == 1:
	print(1)
	print(1)
else:
	X = max(Chains, key=len)
	sys.stdout.write(str(len(X)) + '\n')
	sys.stdout.write(str(X[-1] + 1))
	for i in range(len(X) - 2, -1, -1):
		sys.stdout.write(' ' + str(X[i] + 1))


n = int(input())
t = [0] + list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
(res, cnt) = ([], [0] * (n + 1))
for i in a:
	cnt[i] += 1
for i in range(1, n + 1):
	if t[i] == 0:
		continue
	curr_res = [i]
	x = a[i]
	while cnt[x] == 1:
		curr_res.append(x)
		x = a[x]
	if len(curr_res) > len(res):
		res = curr_res[:]
res.reverse()
print(len(res))
print(*res)