가오리의 코딩일기
[10451] 순열 사이클 본문

import sys
sys.setrecursionlimit(10000) # 재귀 한도 풀어주기
def dfs(start):
visited[start] = True
node = graph[start]
if not visited[node]:
dfs(node)
testCase = int(sys.stdin.readline())
for _ in range(testCase):
n = int(sys.stdin.readline())
graph = [0] + list(map(int, sys.stdin.readline().split()))
visited = [True] + [False] * n
result = 0
for i in range(1, n+1):
if not visited[i]:
dfs(i)
result += 1
print(result)'Python > 백준' 카테고리의 다른 글
| [2178] 미로 탐색 (0) | 2022.08.21 |
|---|---|
| [2667] 단지번호 붙이기 (0) | 2022.08.21 |
| [11724] 연결 요소의 개수 (0) | 2022.08.21 |
| [1406] 에디터 (0) | 2022.08.15 |
| [1158] 요세푸스 문제 (0) | 2022.08.15 |