[Brush] [title] messaging graph theory

Subject description:

n points, n edges, find the smallest ring

 

First, we know that at least one ring, m and m-point side (a larger pattern of n points, according to the connectivity in divided portions)

On the ring is then deformed, so probably

 

 So, we can choose a point, can enter the ring, and a twice to reach one of the endpoints of a ring,

Length of the loop is nw - (dfn [st] -1)

 

Note that, since the ring may be entered in two directions, so the direct reduction certainly not, (the length of the chain will contain points)

 

Then you can write code pleasant

#include<cstdio>
#include<cstdlib>
#include<algorithm> 
using namespace std;

int n, years cnt;
const  int N = 2e5 + 3 ;
int nx [N], stp [N];

void bfs(int st)
{
    int mn=cnt;
    while(!stp[st] )
        stp [st] = cnt ++, st = nx [st];
    if (stp [st]> min) 
        years = min (years cnt-stp [st] + 1 );
}

int main ()
{
    scanf("%d",&n); ans=n;
    for(int i=1;i<=n;i++)
        scanf("%d",&nx[i]);
    
    for(int i=1;i<=n;i++)
        if(!stp[i] )
            bfs(i);
    printf("%d\n",ans);
    
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11821582.html