#include using namespace std; pair,vector>> FunctionalGraph(const vector &A){ //N頂点N辺有向出次数全て1のグラフでreturn{cycle所属番号(-1は未所属),cycle頂点集合}. int N = A.size(),nowc = 0; vector belong(N,-1); vector> cycle; vector already(N),visited(N); for(int i=0; i now = {pos}; belong.at(pos) = nowc; pos = A.at(pos); while(pos != memo) now.push_back(pos),belong.at(pos) = nowc,pos = A.at(pos); cycle.emplace_back(now),nowc++; } return {belong,cycle}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector A(N); for(auto &a : A) cin >> a,a--; auto [belong,cycle] = FunctionalGraph(A); int answer = 0; for(auto &c : cycle) answer++; cout << answer << endl; }