Skip to content

Commit d332d57

Browse files
authored
Create 1751-PlanetsCycles.cpp
steps:- 1. firstly try to find out a cycle and its length then assign every node value equal to cycle length. 2. after that start traverse back to eveynodes parent and ans[par]=ans[child]+1; if(ans[par] not find before); 3. there would be one more case if we traversing node and we have already ans of a child then we started to traverse back from there: ans[par]=ans[child]+1; if(ans[par] not find before);
1 parent 06a82fc commit d332d57

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

cses graph/1751-PlanetsCycles.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain , give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
10+
/*
11+
steps:-
12+
1. firstly try to find out a cycle and its length then assign every node value equal to cycle length.
13+
2. after that start traverse back to eveynodes parent and ans[par]=ans[child]+1; if(ans[par] not find before);
14+
15+
3. there would be one more case if we traversing node and we have already ans of a child then we started to traverse back from there:
16+
ans[par]=ans[child]+1; if(ans[par] not find before);
17+
18+
*/
19+
20+
21+
#include <bits/stdc++.h>
22+
using namespace std;
23+
24+
#define ll long long
25+
#define mod 1000000007
26+
ll n;
27+
28+
29+
vector<ll> graph[200005];
30+
vector<ll> visited(200005,0);
31+
vector<ll> par(200005,-1);
32+
vector<pair<ll,bool>> ans(200005,{0,false});
33+
ll cl=1;
34+
void dfs(ll v){
35+
visited[v]=cl;
36+
for(auto u:graph[v]){
37+
if(visited[u]==0){
38+
par[u]=v;
39+
dfs(u);
40+
}
41+
42+
if(visited[u]==cl){
43+
ll st=v;
44+
ll cyc=0;
45+
while(st!=u){
46+
cyc++;
47+
st=par[st];
48+
}
49+
st=v;
50+
cyc++;
51+
while(st!=u){
52+
ans[st].first=cyc;
53+
st=par[st];
54+
}
55+
// cyc++;
56+
ans[st].first=cyc;
57+
ans[st].second=true;
58+
ll prev=st;
59+
while(st!=-1){
60+
prev=st;
61+
st=par[st];
62+
if(!ans[st].second){
63+
ans[st].first=ans[prev].first+1;
64+
ans[st].second=true;
65+
}
66+
}
67+
}else if(visited[u]!=(-1*cl)){
68+
// cout<<"UES"<<endl;
69+
// cout<<u<<endl;
70+
ll st=v;
71+
ll prev=u;
72+
while(st!=-1 ){
73+
if(!ans[st].second){
74+
ans[st].first=ans[prev].first+1;
75+
ans[st].second=true;
76+
}
77+
prev=st;
78+
st=par[st];
79+
}
80+
81+
82+
}
83+
}
84+
visited[v]=(-1*cl);
85+
}
86+
87+
88+
int main()
89+
{
90+
ios_base::sync_with_stdio(false);
91+
cin.tie(NULL);
92+
93+
// ll n;
94+
cin >> n;
95+
96+
for (ll i = 1; i <=n; i++)
97+
{
98+
ll x;
99+
cin>>x;
100+
101+
graph[i].push_back(x);
102+
}
103+
for(ll i=1;i<=n;i++){
104+
if(visited[i]==0){
105+
dfs(i);
106+
cl++;
107+
}
108+
}
109+
110+
111+
for(ll i=1;i<=n;i++){
112+
cout<<ans[i].first<<" ";
113+
}
114+
cout<<endl;
115+
116+
117+
118+
return 0;
119+
}

0 commit comments

Comments
 (0)