fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll=long long;
  4.  
  5. int main() {
  6. // your code goes here
  7. ll n;
  8. cin>>n;
  9. vector<ll>arr(n+1);
  10. for(ll i=1;i<=n;i++) cin>>arr[i];
  11. unordered_map<ll,ll>mp;
  12. ll ans=0;
  13.  
  14. for(ll j=1;j<=n;j++){
  15. ans+=mp[arr[arr[arr[j]]]];
  16. mp[arr[arr[arr[j]]]]++;
  17.  
  18. }
  19. cout<<ans<<endl;
  20. return 0;
  21. }
Success #stdin #stdout 0s 5324KB
stdin
4 
4 2 1 2
stdout
6