From dc3095d3ce47d62bf1bced19e00efe9a501dc3ec Mon Sep 17 00:00:00 2001 From: jeeminimini Date: Mon, 20 Nov 2023 20:37:52 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=88=AB=EC=9E=90=EA=B3=A0=EB=A5=B4?= =?UTF-8?q?=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\352\263\240\353\245\264\352\270\260.py" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "src/main/kotlin/jimin/50week/\354\210\253\354\236\220\352\263\240\353\245\264\352\270\260.py" diff --git "a/src/main/kotlin/jimin/50week/\354\210\253\354\236\220\352\263\240\353\245\264\352\270\260.py" "b/src/main/kotlin/jimin/50week/\354\210\253\354\236\220\352\263\240\353\245\264\352\270\260.py" new file mode 100644 index 00000000..6776da1a --- /dev/null +++ "b/src/main/kotlin/jimin/50week/\354\210\253\354\236\220\352\263\240\353\245\264\352\270\260.py" @@ -0,0 +1,36 @@ +''' +처음에는 그냥 dfs로 했더니 시간초과났다 + +사이클을 도는 친구를 찾아야한다.. +https://velog.io/@deannn/BOJ-%EB%B0%B1%EC%A4%80-2668%EB%B2%88-%EC%88%AB%EC%9E%90%EA%B3%A0%EB%A5%B4%EA%B8%B0-Python +참고 +''' + +import sys +n = int(sys.stdin.readline()) +numbers = [0] +for i in range(n): + numbers.append(int(sys.stdin.readline())) + +answer = set() + + +def dfs(first, second, now): + first.add(now) + second.add(numbers[now]) + if numbers[now] in first: + if first == second: + answer.update(first) + return + return + return dfs(first, second, numbers[now]) + + +for i in range(1, n + 1): + if i not in answer: + dfs(set(), set(), i) + +print(len(answer)) +for num in sorted(list(answer)): + print(num) +