Advertisement
simushin_pavel

Untitled

Dec 21st, 2020
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. d, f=map(int, input().split())
  2. fs=[]
  3. tb=[]
  4. mmax=0
  5. res=""
  6. fs.append(["", 0])
  7. for i in range(1, d+1):
  8.     s=input()
  9.     name=s.split()[0]
  10.     parent=int(s.split()[1])
  11.     fs.append([name, parent])
  12. tb.append(0)
  13. for i in range(1, f+1):
  14.     tb.append(int(input()))
  15. for i in range(1, d+1):
  16.     c=0
  17.     note=[False]*(f+5)
  18.     u=fs[i][1]
  19.     while u!=-1:
  20.         if not u:
  21.             print(fs[i][0], "BAD")
  22.             exit(0)
  23.         if note[u]:
  24.             print(fs[i][0], "CYCLE")
  25.             exit(0)
  26.         note[u]=True
  27.         c+=1
  28.         u=tb[u]
  29.     if(mmax<c):
  30.         mmax=c
  31.         res=fs[i][0]
  32. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement