fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. int A[n+1]={0};
  9. for(int i=1;i<=n;i++)
  10. {
  11. cin>>A[i];
  12. }
  13.  
  14. unordered_map<int,int>map;
  15. int count=0;
  16. for(int i=1;i<=n;i++)
  17. {
  18. if(map.find(A[A[A[i]]])!=map.end())
  19. {
  20. count+=map[A[A[A[i]]]];
  21. }
  22.  
  23. map[A[A[A[i]]]]++;
  24. }
  25.  
  26. cout<<count;
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
4 2 1 2
stdout
6