2025年6月12日 星期四

小群體

 n = int(input())

d = [int(i) for i in input().split()]

print(d)


v = []

gs = []

for i in d:

  if i not in v:

    g = []

    cur = i

    while cur not in v:

      g.append(cur)

      v.append(cur)

      cur = d[cur]

    gs.append(g)


for g in gs:

  print(g)

print(len(gs))


data

3

0 2 1


Output:

[0, 2, 1]

[0]

[2, 1]

2

沒有留言:

張貼留言