-
Notifications
You must be signed in to change notification settings - Fork 0
/
10451.cpp
55 lines (40 loc) · 1.01 KB
/
10451.cpp
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
49
50
51
52
53
54
55
//#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <memory.h>
using namespace std;
vector<int> adj[1001];
bool visited[1001];
int counter;
void dfs(int here){
visited[here] = true;
for (int i = 0; i < adj[here].size(); i++)
if (visited[adj[here][i]] == false)
dfs(adj[here][i]);
return;
}
int main(void){
int Test_Case, N, tmp;
scanf("%d", &Test_Case);
for (int i = 1; i <= Test_Case; i++){
//초기화
for (int j = 0; j < 1001; j++)
adj[j].clear();
memset(visited, false, sizeof(visited));
counter = 0;
scanf("%d", &N);
for (int j = 1; j <= N; j++){
scanf("%d", &tmp);
adj[j].push_back(tmp);
adj[tmp].push_back(j);
}
for (int i = 1; i <= N; i++){
if (visited[i] == false){
dfs(i);
counter++;
}
}
printf("%d\n", counter);
}
return 0;
}