This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address.
连临街表都不用,每个节点的出度为1,数组搞定访问
#include#include using namespace std;#define N 200010int GI(){ int x=0,c=getchar(),f=0; while(c<'0'||c>'9'){ if(c=='-')f=1;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return f?-x:x;}int dfn[N],low[N],s[N],in[N],idx,top,ans=1<<30,to[N];void tarjan(int u){ dfn[u]=low[u]=++idx; s[++top]=u,in[u]=1; int v=to[u]; if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]); else if(in[v]) low[u]=min(low[u],dfn[v]); if(low[u]==dfn[u]){ int sz=0; do++sz,in[top]=0; while(s[top--]!=u); if(sz>1)ans=min(ans,sz); }}int main(){ int n=GI(),u,v; for(v=1;v<=n;v++)to[v]=GI(); for(u=1;u<=n;u++)tarjan(u); return printf("%d",ans),0;}