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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <cstring> //memset using namespace std; const int MAX = 1000 + 1; int main(void) { int N; cin >> N; while (N--) { vector<int> arr[MAX]; bool visited[MAX]; memset(visited, false, sizeof(visited)); int s, k, count = 0; cin >> s; for (int j = 1; j <= s; j++) { cin >> k; arr[j].push_back(k); } for(int i=1;i<=s;i++){ int again =i; int x = i; if (!visited[x]) { //숫자 i를 들렸었는가? visited[x] = true; //들렸으면 들린거 체크 while (arr[x][0] != again) { //i를 들리지 않았으면 1->3->7->5->1 1이 다시나올때까지 루프돌면서 들린곳 다체크 x = arr[x][0]; visited[x] = true; } count++; //루프돌리고 순환한 사이클 +1 } } cout << count<<"\n"; } return 0; } | cs |
인접리스트로 구현해봤습니다~~
문제 출처:https://www.acmicpc.net/problem/10451
문제 피드백,질문은 댓글로 해주세요~!
'알고리즘 > BAEKJOON' 카테고리의 다른 글
백준 2178번 미로 탐색 (0) | 2018.11.22 |
---|---|
백준 11724번 연결 요소의 개수 (0) | 2018.11.09 |
백준 1718번 암호 (0) | 2018.11.06 |
백준 1972번 놀라운 문자열 (0) | 2018.11.06 |
백준 4334번 평균은 넘겠지 (0) | 2018.11.01 |