fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. void solve(){
  7. int n;
  8. cin>>n;
  9. vector<int>v(n+1);
  10. for(int i=1;i<=n;i++)cin>>v[i];
  11. unordered_map<int,int>mp;
  12. for(int i=1;i<=n;i++) mp[v[v[v[i]]]]++;
  13. ll p=0;
  14. for(auto &i:mp){
  15. ll a=i.second;
  16. p+=(a*(a-1))/2;
  17. }
  18. cout<<p<<"\n";
  19. }
  20.  
  21. int main(){
  22. ios_base::sync_with_stdio(false);
  23. cin.tie(NULL);
  24. solve();
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5284KB
stdin
4
4 2 1 2
stdout
6