fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(int a[],int n){
  4. int count=0;
  5.  
  6. for(int i=1;i<=n;i++){
  7. for(int j=i+1;j<=n;j++){
  8. if(a[a[a[i]]]==a[a[a[j]]]){
  9. count++;
  10. }
  11. }
  12. }
  13. return count;
  14. }
  15.  
  16. int main() {
  17. // your code goes here
  18. int Q;
  19. cin>>Q; //no of queries
  20. int n;
  21. cin>>n; //no of elements
  22. int arr[n+1];
  23. for(int i=1;i<=n;i++){
  24. cin>>arr[i];
  25. }
  26. int i=1;
  27. while(i<=Q){
  28. cout<<"The count of special pair is:"<<getCount(arr,n);
  29. i++;
  30. }
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5280KB
stdin
1
4
4 2 1 2
stdout
The count of special pair is:6