Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d, f=map(int, input().split())
- fs=[]
- tb=[]
- mmax=0
- res=""
- fs.append(["", 0])
- for i in range(1, d+1):
- s=input()
- name=s.split()[0]
- parent=int(s.split()[1])
- fs.append([name, parent])
- tb.append(0)
- for i in range(1, f+1):
- tb.append(int(input()))
- for i in range(1, d+1):
- c=0
- note=[False]*(f+5)
- u=fs[i][1]
- while u!=-1:
- if not u:
- print(fs[i][0], "BAD")
- exit(0)
- if note[u]:
- print(fs[i][0], "CYCLE")
- exit(0)
- note[u]=True
- c+=1
- u=tb[u]
- if(mmax<c):
- mmax=c
- res=fs[i][0]
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement