-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10451.cpp
More file actions
50 lines (41 loc) · 706 Bytes
/
Copy path10451.cpp
File metadata and controls
50 lines (41 loc) · 706 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> graph[1001];
int visited[1001];
int cnt = 0;
void dfs(int here) {
visited[here] = 1;
for (auto v : graph[here]) {
if (visited[v] == 1) {
cnt++;
}
if (visited[v] != 1) {
dfs(v);
}
}
}
int main(void) {
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
int N;
scanf("%d", &N);
for (int i = 0; i<= N; i++) {
graph[i].assign(0, 0);
}
for (int i = 1; i <= N; i++) {
int v;
scanf("%d", &v);
graph[i].push_back(v);
}
cnt = 0;
fill(visited, visited + N + 1, 0);
for (int i = 1; i <= N; i++) {
if(visited[i] != 1)
dfs(i);
}
printf("%d\n", cnt);
}
}