ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 10451 C++ 풀이
    카테고리 없음 2018. 11. 18. 17:47


    순열 사이클의 정의가 좀 모호해서 짜증났는데,

    적당히 엉성하게 플밍했더니 통과되었습니다.


    무슨 정의를 예시로 해... 

    문제 별로에요.


    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <climits>
    using namespace std;

    int buffer[1001];
    bool isVisited[1001];
    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    for(auto k=0; k<t; k++){
    int n;
    cin >> n;
    for (auto i=1; i<=n; i++){
    cin >> buffer[i];
    }
    for (auto i=1; i<=n; i++){
    isVisited[i]=false;
    }
    int cycle = 0;

    for (auto i=1; i<=n; i++){
    if (!isVisited[i]){
    cycle++;
    int next = buffer[i];
    while (!isVisited[next]){
    isVisited[next] = true;
    next = buffer[next];
    }

    }
    }
    cout << cycle << '\n';
    }
    return 0;

    }


Designed by Tistory.