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: interview

Starter code:

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


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


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

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


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': # it's hotel
        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])

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]))
    # adj = [list() for _ in range(n)]
    # visited = [0] * (n + 1)
    # best = -1
    # start = -1
    # bestlen = 0
    # for i in range(n):
    #     if a[i] > 0: adj[a[i] - 1].append(i)
    # # counter = 0
    # for i in range(n):
    #     if visited[i] == 0:
    #         cur = i
    #         # temp = list()
    #         # temp.append(cur)
    #         temp = 1
    #         visited[cur] += 1
    #         while len(adj[cur]) == 1:
    #             # print(cur)
    #             if len(adj[cur]) > 0 and adj[cur][0] == cur: break
    #             cur = adj[cur][0]
    #             # temp.append(cur)
    #             visited[cur]+=1
    #             temp+=1
    #             # if counter == 10000: return - 1
    #         if types[cur] == 1 and temp > bestlen:
    #             best = cur
    #             bestlen = temp
    #             start = i
    # res = list()
    # while True:
    #     res.append(best)
    #     if best == start: break
    #     next = a[best] - 1
    #     best = next
    # res = res[::-1]
    # for i in range(len(res)): res[i] += 1
    # print(len(res))
    # return ' '.join(map(str, res))

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())

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


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



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

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



# Made By Mostafa_Khaled


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)