Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. def look_at(i, looked_at, permutations):
  2. if(looked_at[i]):
  3. return looked_at
  4. looked_at[i] = True
  5. return look_at(permutations[i] - 1, looked_at, permutations)
  6.  
  7. def countCycles(n, permutations):
  8. looked_at = [False] * n
  9. count = 0
  10. for i in range(n):
  11. if not (looked_at[i]):
  12. count += 1
  13. # print(i + 1)
  14. looked_at = look_at(i, looked_at, permutations)
  15. return count
  16.  
  17. import sys
  18.  
  19. data = sys.stdin.readlines()
  20.  
  21. # data = ['2\n', '8\n', '3 2 7 8 1 4 5 6\n', '10\n', '2 1 3 4 5 6 7 9 10 8\n']
  22. # print(len(data))
  23. # if len(data) == 0:
  24. # print('???')
  25. # return
  26. n = int(data[0])
  27. for i in range (1, n + 1):
  28. # print(int(data[2*i - 1]), [int(s) for s in data[2*i].strip('\n').split(' ')])
  29. print(countCycles(int(data[2*i - 1]), [int(s) for s in data[2*i].strip('\n').split(' ')]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement