728x90
๋ฌธ์
ํ์ด
์ฌ์ดํด์ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
์๋ ํ์ด์ ๊ฑฐ์ ๋์ผํ๋ค.
https://ku-hug.tistory.com/169
์ฝ๋
import sys
input = sys.stdin.readline
n = int(input())
graph = [0]
visited = [False] * (n + 1)
cycle = []
ans = []
for _ in range(n):
graph.append(int(input()))
def dfs(x):
visited[x] = True
cycle.append(x)
next = graph[x]
if visited[next]:
if next in cycle:
for c in cycle[cycle.index(next):]:
ans.append(c)
return
dfs(next)
for i in range(1, n + 1):
if not visited[i]:
cycle = []
dfs(i)
print(len(ans))
ans.sort()
for a in ans:
print(a)
๋ง๋ฌด๋ฆฌ
์ฌ์ดํด ๋ฌธ์ ๋ฅผ ํ๋ฒ ํ์ ํด์ ์ฝ๊ฒ ํ ์ ์์๋ค.
728x90