일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 분할 정복
- syscall
- 포맷스트링버그
- 완전 탐색
- DFS
- 에라토스테네스의 체
- 백트래킹
- 연결리스트
- 문자열 처리
- BFS
- tcache
- heap
- 투 포인터
- RTL
- ROP
- 수학
- 큐
- 스위핑 알고리즘
- 이진트리
- 동적 계획법
- 이진 탐색
- off by one
- House of Orange
- 이분 탐색
- OOB
- 브루트 포스
- 다이나믹 프로그래밍
- 스택
- BOF
- fsb
Archives
- Today
- Total
SDJ( 수돈재 아님 ㅎ )
[C++] 10451 - 순열 사이클 본문
문제 링크 : 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& 2&7&8&1&4&5&6 \end{pmatrix}\) 와 같다. 또는, Figure 1과 같이 방향 그래프로 나타낼 수도 있다. 순열을 배열을 이용해 \(\begin{pmatrix} 1
www.acmicpc.net
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include<iostream>
#include<algorithm>
#include<cstring> // for memset
#define endl '\n'
using namespace std;
int cycle[1005];
bool memo[1005];
int T;
int dfs(int start, int end, int n)
{
for(int i = 1; i <= n; ++i)
{
end = cycle[end];
memo[end] = 1;
if(start == end)
return 1;
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
int cnt;
cin >> T;
for(int i = 0; i < T; ++i)
{
cnt = 0;
cin >> n;
for(int j = 1 ; j <= n; ++j)
cin >> cycle[j];
for(int j = 1; j <= n; ++j)
if(!memo[j])
cnt += dfs(j, j, n);
cout << cnt << endl;
memset(cycle, 0, sizeof(cycle));
memset(memo, 0, sizeof(memo));
}
return 0;
}
|
'알고리즘 > Backjoon' 카테고리의 다른 글
[Python3] 16212 - 정열적인 정렬 (0) | 2020.01.05 |
---|---|
[Python3] 2355 - 시그마 (0) | 2020.01.05 |
[Python3] 2935 - 소음 (0) | 2020.01.05 |
[C++] 15657 - N과 M (8) (0) | 2020.01.05 |
[C++] 15656 - N과 M (7) (0) | 2020.01.05 |
Comments