-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1020B.cpp
More file actions
48 lines (39 loc) · 881 Bytes
/
1020B.cpp
File metadata and controls
48 lines (39 loc) · 881 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
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fo(i,n) for(int i = 0; i < n; i++)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
const int MAX = 1e3+5;
vector<vector<int>>grafo(MAX);
int dfs(vector<bool>& vis, int v) {
vis[v] = true;
for(auto u : grafo[v]) {
if(vis[u]) {
return u;
}
int resultado = dfs(vis, u);
if(resultado != -1) {
return resultado;
}
}
return -1;
}
int main() {_
int n;
cin >> n;
fo(i,n){
int x;cin >> x;
grafo[i+1].push_back(x);
}
for(int a = 1; a <=n; a++) {
vector<bool>vis(MAX);
int ciclo = dfs(vis,a);
if(ciclo != -1) {
cout << ciclo <<" ";
}
}
return 0;
}