耗时两年半完成的题解
2023-07-12 11:07:30
发布于:广东
50阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 200010;
int n, fa[N], ans = 0x3f3f3f3f;
int get (int x, int &cnt) { //cnt记录环的长度
cnt ++;
if (fa[x] == x) return x;
else return get(fa[x], cnt);
}
int main () {
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
fa[i] = i;
for (int i = 1; i <= n; i ++) {
int cnt = 0, f;
scanf("%d", &f);
if (get(f, cnt) == i) {
ans = min(ans, cnt);
}else
fa[i] = f;
}
printf("%d", ans);
return 0;
}
全部评论 1
7
2023-07-12 来自 广东
1
有帮助,赞一个