0%

[題解]小群體

a103. 小群體

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
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define lld long long
#define N 50005
using namespace std;

signed main(){
int n,arr[N];cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
bool used[N];
int ans = 0;
memset(used,0,sizeof(used));

for(int i=0;i<n;i++){
if(used[i])continue;
int cur = arr[i];
used[cur] = 1;
while(cur!=i){
cur = arr[cur];
used[cur] = 1;
}
ans++;
}
cout<<ans<<endl;
}