Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def look_at(i, looked_at, permutations):
- if(looked_at[i]):
- return looked_at
- looked_at[i] = True
- return look_at(permutations[i] - 1, looked_at, permutations)
- def countCycles(n, permutations):
- looked_at = [False] * n
- count = 0
- for i in range(n):
- if not (looked_at[i]):
- count += 1
- # print(i + 1)
- looked_at = look_at(i, looked_at, permutations)
- return count
- import sys
- data = sys.stdin.readlines()
- # 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']
- # print(len(data))
- # if len(data) == 0:
- # print('???')
- # return
- n = int(data[0])
- for i in range (1, n + 1):
- # print(int(data[2*i - 1]), [int(s) for s in data[2*i].strip('\n').split(' ')])
- 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