// Copyright © 2021 ์์ฃผํ. All rights reserved.
// https://github.com/dkswnkk
// https://www.acmicpc.net/problem/10451
// BOJ10451 ์์ด ์ฌ์ดํด
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
int graph[1001];
int visited[1001];
bool check;
void dfs(int start,int next){
visited[start]=1;
visited[next]=1;
if(next==start){
check= true;
return;
}
else{
dfs(start,graph[next]);
}
}
int main(){
int T; cin>>T;
while(T--){
int ans=0;
int N; cin>>N;
for(int i=1; i<=N; i++){
cin>>graph[i];
}
for(int i=1; i<=N; i++){
if(!visited[i])dfs(i,graph[i]);
if(check){
ans++;
check=false;
}
}
cout<<ans<<'\n';
memset(visited, 0, sizeof(visited));
}
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 10773๋ฒ - ์ ๋ก (0) | 2021.10.23 |
---|---|
[๋ฐฑ์ค,c++] 10718๋ฒ - We love kriii (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 10610๋ฒ - 30 (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1032๋ฒ - ๋ช ๋ น ํ๋กฌํํธ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 1026๋ฒ - ๋ณด๋ฌผ (0) | 2021.10.16 |
[๋ฐฑ์ค,c++] 10174๋ฒ - ํฐ๋ฆฐ๋๋กฌ (0) | 2021.10.16 |
๋๊ธ