Created
July 25, 2017 12:32
-
-
Save shreyansh26/ba97f0273b9f7e55c0be5178fd1bc464 to your computer and use it in GitHub Desktop.
temp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int main() { | |
int n; | |
cin>>n; | |
int a[n]; | |
bool b[n]; | |
fill(b, b + n, true); | |
for(int i=0; i<n; i++) { | |
cin>>a[i]; | |
} | |
for(int i=0; i<n; i++){ | |
int j = i; | |
int flag = 0; | |
vector<int> v; | |
if(b[j]) { | |
flag = 1; | |
v.push_back(j+1); | |
j = a[j]-1; | |
} | |
while(b[j]) { | |
v.push_back(j+1); | |
b[j] = false; | |
j = a[j]-1; | |
} | |
if(flag) { | |
for(int k=0; k<v.size(); k++) { | |
cout<<v[k]<<" "; | |
} | |
cout<<"\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment