EPS/11-03-24/b.py

43 lines
780 B
Python
Raw Normal View History

2024-03-25 15:27:12 +01:00
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()