-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2668.cpp
More file actions
42 lines (42 loc) · 858 Bytes
/
2668.cpp
File metadata and controls
42 lines (42 loc) · 858 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int arr[101];
int visited[101];
vector<int> res;
void init() {
for(int i=0; i<101; ++i){
if(visited[i]!=2) visited[i]=0;
}
}
void dfs(int x){
visited[x]=1;
if(visited[arr[x]]==1){
//arr[x]부터 x까지 사이클이 생김.
int a=arr[x];
while(a!=x){
res.push_back(a);
visited[a]=2;
a=arr[a];
}
res.push_back(a);
visited[a]=2;
return;
}
if(visited[arr[x]]==0) dfs(arr[x]);
}
int main(){
int N; cin>>N;
for(int i=1; i<=N; ++i){
cin>>arr[i];
}
for(int i=1; i<=N; ++i){
if(visited[i]==2) continue;
init();
dfs(i);
}
sort(res.begin(), res.end());
cout << res.size() << '\n';
for(auto it:res){
cout << it << '\n';
}
}