43 lines
780 B
Python
43 lines
780 B
Python
def main():
|
|
n = int(input())
|
|
fa = [int(i)-1 for i in input().split()]
|
|
est_feuille = [True for _ in range(n)]
|
|
for i in range(n):
|
|
if fa[i] != i:
|
|
est_feuille[fa[i]] = False
|
|
|
|
f_count = 0
|
|
for i in range(n):
|
|
if est_feuille[i]:
|
|
f_count += 1
|
|
|
|
print(f_count)
|
|
|
|
used = set()
|
|
def find_path(s):
|
|
l = []
|
|
length = 0
|
|
while s not in used:
|
|
l.append(s)
|
|
used.add(s)
|
|
length += 1
|
|
s = fa[s]
|
|
return reversed(l), length
|
|
|
|
|
|
paths = []
|
|
for i in range(n):
|
|
if est_feuille[i]:
|
|
p, n = find_path(i)
|
|
|
|
print(n)
|
|
print(" ".join([str(i+1) for i in p]))
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
|
|
|
for _ in range(int(input())):
|
|
main() |