-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10451.cpp
47 lines (43 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
#include <bits/stdc++.h>
using namespace std;
int t, n, a[1010], cnt, visited[1010], ans;
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while(t--){
cnt = 0, ans = 0;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
while(cnt != n){
int i = 0;
for(i = 1; i <= n; i++){
if(!visited[i]) break;
}
int ptr = a[i];
if(ptr == i){
ans++;
cnt++;
visited[i] = 1;
}
else{
visited[i] = 1;
visited[ptr] = 1;
cnt += 1;
while(ptr != i){
ptr = a[ptr];
visited[ptr] = 1;
cnt++;
}
ans++;
}
}
cout << ans << "\n";
for(int i = 1; i <= n; i++){
visited[i] = 0;
}
}
}