Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- graph
- String
- C언어
- 생활코딩
- web
- udemy
- Python
- BASIC
- sorting
- 따배씨
- BFS
- server
- programmers
- BOJ
- C
- 종만북
- 인프런
- 정수론
- Cleancode
- JavaScript
- greedy
- DP
- 백준
- Math
- Algospot
- Algorithm
- php
- dfs
- 따라하며 배우는 C언어
- 따라하면서 배우는 C언어
Archives
- Today
- Total
몽상실현개발주의
[BOJ] 10451 / 순열 사이클 / Python 파이썬 본문
[BOJ] 10451 / 순열 사이클 / Python 파이썬
https://www.acmicpc.net/problem/10451
10451번: 순열 사이클
1부터 N까지 정수 N개로 이루어진 순열을 나타내는 방법은 여러 가지가 있다. 예를 들어, 8개의 수로 이루어진 순열 (3, 2, 7, 8, 1, 4, 5, 6)을 배열을 이용해 표현하면 \(\begin{pmatrix} 1 & 2 &3&4&5&6&7&8 \\ 3
www.acmicpc.net
풀이
순열과 index 로 그려진 graph 의 사이클 수를 구하는 문제이다.
순열의 수가 다음 순열의 index 가 된다는 것을 이해하면 쉽게 구할 수 있다.
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
N = int(input())
nums = list(map(int, input().split()))
nums = [0] + nums
cnt = 0
for f in range(1, N+1):
if nums[f] < 0:
continue
cnt += 1
t = nums[f]
nums[f] = -1
while True:
if t < 0:
break
f = t
t = nums[f]
nums[f] = -1
print(cnt)
'Algorithm PS > BOJ' 카테고리의 다른 글
[BOJ] 9466 / 텀 프로젝트 / Python 파이썬 (0) | 2021.06.09 |
---|---|
[BOJ] 2331 / 반복수열 / Python 파이썬 (0) | 2021.06.07 |
[BOJ] 1707 / 이분 그래프 / Python 파이썬 (0) | 2021.06.07 |
[BOJ] 11724 / 연결 요소의 개수 / Python 파이썬 (0) | 2021.06.05 |
[BOJ] 1260 / DFS와 BFS / Python 파이썬 (0) | 2021.06.05 |
Comments