Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- int d;
- cin>>d;
- for (int a = 0; a < d; a++)
- {
- int s[10001];
- int c[10001];
- int t[10001];
- int l,z,n,d;
- l=0;
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- cin>>t[i];
- s[i]=0;
- c[i]=0;
- }
- for(int i=1;i<=n;i++)
- {
- z=i;
- for(int j=1;!s[z];j++)
- {
- s[z]=i;
- z=t[z];
- }
- if(s[z]==i)
- while(!c[z])
- {
- c[z]=1;
- l++;
- z=t[z];
- }
- }
- cout<< l << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement