40 lines
719 B
C++
40 lines
719 B
C++
|
#include <iostream>
|
||
|
#include <queue>
|
||
|
const int MAX_N{int(1e5+5)};
|
||
|
|
||
|
int in_degree[MAX_N]{},n,next[MAX_N];
|
||
|
std::queue<int> q;
|
||
|
|
||
|
void find(int num){
|
||
|
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
std::cin>>n;
|
||
|
for(int i=1;i<=n;i++){
|
||
|
int input;
|
||
|
std::cin>>input;
|
||
|
in_degree[input]++;
|
||
|
next[i]=input;
|
||
|
}
|
||
|
int zero_in_degree_dir = -1;
|
||
|
int cnt{};
|
||
|
for(int i=1;i<=n;i++){
|
||
|
if(in_degree[i]==0){
|
||
|
q.push(i);
|
||
|
cnt++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
while(q.empty()==false){
|
||
|
int top = q.front();
|
||
|
in_degree[next[top]]--;
|
||
|
q.pop();
|
||
|
if(in_degree[next[top]]==0){
|
||
|
q.push(next[top]);
|
||
|
cnt++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::cout<<(n-cnt)<<"\n";
|
||
|
}
|