#include #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define RREP(i, n) for(int i = (int)(n); i-- > 0;) #define FOR(i, l, r) for(int i = (int)(l); i < (int)(r); ++i) #define RFOR(i, l, r) for(int i = (int)(r); i-- > (int)(l);) #define ALL(v) std::begin(v), std::end(v) using llong = long long; using vi = std::vector; using vvi = std::vector; using pii = std::pair; using namespace std; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr double EPS = 1e-10; constexpr int MOD = 998'244'353; constexpr int MOD2 = 1e9 + 7; template > requires std::ranges::output_range inline auto &operator>>(std::basic_istream &is, R &r) { for(auto &elem : r) is >> elem; return is; } template requires(!std::convertible_to) inline auto &operator<<(std::basic_ostream &os, const R &r) { if(std::ranges::empty(r)) return os; auto iter = std::ranges::cbegin(r); const auto end = std::ranges::cend(r); os << *iter++; while(iter != end) os << " " << *iter++; return os; } #ifdef DEBUG #include #else #define debug(...) static_cast(0) #endif int main(){ int n; cin>>n; vi p(n); vi deg(n,0); for(auto &e:p){ cin>>e; if(e!=0) deg[e-1]++; } debug(deg); vi depth(n,-1); queue que; REP(i,n){ if(deg[i]>0) continue; depth[i]=0; que.push(i); } while(!que.empty()){ auto u=que.front(); que.pop(); if(p[u]==0) continue; auto v=p[u]-1; if(depth[v]!=-1) continue; depth[v]=depth[u]+1; que.push(v); } int ans=0; for(auto e:depth){ if(e==2) ans++; } cout<