-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18960f4
commit dc3095d
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|