Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2e5 + 10;
- int n, a[N];
- int vis[N];
- int dep[N];
- int ans = N;
- void dfs (int x, int d, int v) {
- // cout << x << ' ' << d << ' ' << dep[x] << endl;
- if (vis[x]) {
- if (v == vis[x]) ans = min(ans, d - dep[x]);
- return;
- }
- vis[x] = v;
- dep[x] = d;
- dfs(a[x], d + 1, v);
- }
- int main () {
- cin >> n;
- for (int i = 1; i <= n; i ++) {
- cin >> a[i];
- }
- for (int i = 1; i <= n; i ++) {
- if (vis[i]) continue;
- dfs(i, 1, i);
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement