信息传递
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<math.h> #include<queue> #include<vector> using namespace std; int n; int to[210000],dfn[210000],ans=999999,cnt=0,cntt=0,t[210000]; void dfs(int x) { t[x]=cntt; dfn[x]=++cnt; if(dfn[to[x]]) if(cntt==t[to[x]]) {ans=min(dfn[x]-dfn[to[x]]+1,ans);return ;} else return; dfs(to[x]); return ; } int main() { scanf("%d",&n); int c; for(int i=1;i<=n;i++) scanf("%d",&c),to[i]=c; for(int i=1;i<=n;i++) if(!dfn[i]) cntt++,dfs(i); cout<<ans; return 0; }