Study/Baekjoon

[백준/C++] 10451 순열 사이클

에린_1 2024. 3. 3. 22:39
728x90

10451 순열 사이클

#include <bits/stdc++.h>
using namespace std;
vector<int> adj(1001);
int visited[1001];

void dfs(int x)
{
    visited[x] = 1;
    if (!visited[adj[x]])
    {
        dfs(adj[x]);
    }
}
int main() 
{
    int test_case, node, max = 0, check;
    cin >> test_case;

    for (int i = 0; i < test_case; ++i)
    {
        cin >> node;
        memset(visited, 0, sizeof(visited));
        for (int j = 1; j <= node; ++j)
        {
            int tmp;
            cin >> tmp;
            adj[j] = tmp;
        }
        check = 0;        
        for (int j = 1; j <= node; ++j)
        {
            if (!visited[j])
            {
                dfs(j);
                ++check;
            }
        }
        cout << check << '\\n';
    }
}
  • DFS를 사용해서 풀었다.
  • 사이클을 구하는 문제.
  • 순열 사이클을 알아보면 쉽게 풀 수 있다.
728x90