Euler Euler Road &&

  T1 is the Euler Road board, but I will not, directly explode. .

This thing is a the DFS , but I had always thought Euler road only $ O (nm) $ demand

Before we know today can $ O (n + m) $

Euler Road determine:

Undirected: the starting point for the end point of singularity, even the rest of

Directed: a start point of the end point, respectively a poor, remaining equal.

Similar Euler tour.

How demand? ?

We can easily find the starting point. Since then the number of degrees the other point is even, you can ensure that you will be able to go out.

So blind search on the bin , noticed a ring collar, we extend the first point and then finish the points stack.

Code implementation is very simple:

 

void dfs(int x)
{
  for(int i=head[x];i;i=nxt[i])
    
if(!v[id[i]])
    {       v[id[i]]=1;
      dfs1(to[i]);
      s[++top]=id[i];     }  return ; }

 

A little optimization:

Current arc optimization, noted that some people question the cancer card this practice.

Because the expansion of a point more than once, there is no guarantee that the complexity of the simplest data is directly attached to two points m edges.

Therefore, the invention has such a big brother optimized:

One side is visited useless, so we can directly put a header to the first legitimate, that is, the removal of useless state.

Code:

void dfs(int x)
{
    int i=head[x];
    while(i)
    {
        while(i&&v[id[i]]!=0) i=nxt[i];
        head[x]=i;
        if(i)
        {
            v[id[i]]=1;
            dfs(to[i]);
            s[++top]=id[i];
            i=head[x];
        }
    }
    return ;
}

Guess you like

Origin www.cnblogs.com/hzoi-kx/p/11795099.html