#include<iostream>
#include<cstdlib>
using namespace std;
int main (){
int n,f,sum,num=0;
cin >> n;
int friends[n-1],flag[n-1];
for (int j=0;j<n;j++){
cin >> f;
friends[j]=f;
}
for (int j=0;j<n;j++){
flag[j]=0;
}
for (int i=0;i<n;i++){
num=i;
if(flag[num]==0){
while(flag[num]!=1){
flag[num]=1;
num=friends[num];
if (flag[num]==1){
sum++;
}
}
}
}
cout << sum;
return 0;
}
#include
#include
using namespace std;
int main (){
int n,f,sum,num=0;
cin >> n;
int friends[n-1],flag[n-1];
for (int j=0;j
cin >> f;
friends[j]=f;
}
for (int j=0;j
flag[j]=0;
}
for (int i=0;i
num=i;
if(flag[num]==0){
while(flag[num]!=1){
flag[num]=1;
num=friends[num];
if (flag[num]==1){
sum++;
}
}
}
}
cout << sum;
return 0;
}
我怕被質疑貼程式碼,所以傳私人訊息給你了