Skip to content

Instantly share code, notes, and snippets.

@shreyansh26
Created July 25, 2017 12:32
Show Gist options
  • Save shreyansh26/ba97f0273b9f7e55c0be5178fd1bc464 to your computer and use it in GitHub Desktop.
Save shreyansh26/ba97f0273b9f7e55c0be5178fd1bc464 to your computer and use it in GitHub Desktop.
temp
#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