Advertisement
Guest User

Untitled

a guest
Aug 24th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1.     def me(a):
  2.      
  3.         c = [-1]*len(a)
  4.         n = -101
  5.         for i in range(len(a)):
  6.             if c[i]==-1:
  7.                 r = i
  8.                 while c[r]==-1:
  9.                     c[r] = n
  10.                     r = a[r]
  11.                 if c[r]==n:
  12.                     y = i
  13.                     while y!=r:
  14.                         c[y] = r
  15.                         y = a[y]
  16.                 elif c[r]<=-101:
  17.                     y = i
  18.                     while y!=r:
  19.                         c[y] = r
  20.                         y = a[y]
  21.                 elif c[r]>=0:
  22.                     y = i
  23.                     while y!=r:
  24.                         c[y] = c[r]
  25.                         y = a[y]
  26.      
  27.      
  28.                 n -= 1
  29.         for i in range(len(c)):
  30.             if c[i]<=-101:
  31.                 c[i] = i
  32.      
  33.         return (' '.join(str(x+1) for x in c))
  34.      
  35.      
  36.     input()
  37.     a=[int(x)-1 for x in input().split(' ')]
  38.     print(me(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement