-
Notifications
You must be signed in to change notification settings - Fork 10
/
1249B1.cpp
60 lines (55 loc) · 845 Bytes
/
1249B1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
*Author : Akansha
*23/10/19
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n+5];
for(int i=0;i<n;i++){
cin>>a[i];
}
int l[n+5]={0};
map<int,bool> visited;
for(int i=1;i<=n;i++){
int count=0;
int per=a[i-1];
//cout<<"per is "<<per;
vector<int> v;
//cout<<"ok2\n";
if(!visited[i]){
//cout<<"ok1\n";
while(1){
if(per==i){
count++;
v.push_back(i);
visited[i]=true;
break;
}
else{
//cout<<per<<" ";
per=a[per-1];
count++;
v.push_back(per);
visited[per]=true;
}
}
//cout<<endl;
for(int i=0;i<v.size();i++){
l[v[i]-1]=count;
}
}
v.clear();
}
for(int i=0;i<n;i++){
cout<<l[i]<<" ";
}
cout<<endl;
}
return 0;
}