显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<vector>
#include<stack>
#define syy myson
using namespace std;
const int inf=200005;
int n,x;
vector<int>a[inf];
stack<int>s;
int low[inf],scc[inf],pre[inf],scc_cnt,scc_time,ans,mi=10000000;
inline void dfs(int x)
{
low[x]=pre[x]=scc_time++;
s.push(x);
for(int i=0;i<a[x].size();i++)
{
int u=a[x][i];
if(!pre[u])dfs(u),low[x]=min(low[x],low[u]);
else if(!scc[u])low[x]=min(low[x],low[u]);
}
if(pre[x]==low[x])
{
int ans=0;
scc_cnt++;
for(;;)
{
int r=s.top();
s.pop();
scc[r]=scc_cnt;
ans++;
if(x==r)break;
}
if(ans>1)mi=min(mi,ans);
}
}
int Main()
{
freopen("2015message.in","r",stdin);freopen("2015message.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
a[i].push_back(x);
}
for(int i=1;i<=n;i++)
if(!pre[i])dfs(i);
printf("%d",mi);
return 0;
return 0;
}
int main(){;}
int syy=Main();