-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10451.cpp
More file actions
32 lines (29 loc) · 695 Bytes
/
10451.cpp
File metadata and controls
32 lines (29 loc) · 695 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
#include <bits/stdc++.h>
using namespace std;
int arr[1001];
bool visited[1001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
int ans = 0;
memset(arr, 0, sizeof(arr));
memset(visited, 0, sizeof(visited));
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
int cur = i;
while (!visited[cur]) {
visited[cur] = true;
cur = arr[cur];
}
ans++;
}
}
cout << ans << "\n";
}
}