Euler road problems (study notes)

Euler road problem, commonly known as ( "one stroke" problem)

Definition: Given an undirected graph, if there is one from \ (S \) to \ (T \) path just do not leak past the edge of each one (may be repeated through the nodes in the graph), the the path is called \ (S \) to \ (T \) Euler road.

In particular, if the presence of a \ (S \) path starting, just do not leak past the edge of each one (may be repeated through the nodes in the graph), eventually returned to the starting point \ (S \) , the path Euler circuit.

No presence of Euler Euler FIG referred to FIG.

FIG Euler determination: an undirected graph Euler, if and only if no communication to the FIG., And the degree of each point is an even number.

Existence of Euler path determination: the presence of a non-directed graph Eulerian path, if and only if no communication to the view, and FIG degrees in exactly two nodes is an odd number, an even number of degrees that the other nodes. two odd-degree point is the starting point of the Euler path \ (S \) and end \ (T \) .

inline void add(int a,int b){
    nxt[++tot]=head[a];head[a]=tot;to[tot]=b;
}
inline void oula(){
    st[++top]=1;//入栈
    while(top>0){
        int u=st[top],i=head[u];
        while(i&&visit[i])i=nxt[i];//找到一条没有被访问过的边
        if(i){
            st[++top]=to[i];
            visit[i]=visit[i^1]=1;
//标记为访问过,这里体现了tot=1的好处,每一条边被两个方向存储
//数组下标一定是2,3或者4,5这种异或1能够互相得到的
            head[u]=nxt[i];
        }
        else --top,ans[++sum]=u;
//u相连的所有边均被访问过,则回溯.
    }
}
int main(){
    int n=read(),m=read();tot=1;
    for(int i=1;i<=m;++i){
        int a=read(),b=read();
        add(a,b);add(b,a);
    }
    oula();
    for(int i=sum;i>=1;--i)printf("%d\n",ans[i]);//倒序输出
}

POJ , to a template title to practice your hand.

The meaning of problems: given \ (N (N <= 10000 ) \) points \ (M (M <= 50000 ) \) without edges to FIG seek a path from node 1, and finally back to the node 1 and satisfies each edge exactly once through the forward and reverse directions, respectively. if a variety of programs, either to the output.

Analysis: Euler circuit issues on the basis of the template on the removed \ (Visit \) tag array to ensure that each edge happens to be both positive and negative directions through the primary.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
const int N=100005;
int n,m;
int top,sum,st[N],ans[N];
int tot,head[N],nxt[N],to[N];
inline void add(int a,int b){
    nxt[++tot]=head[a];head[a]=tot;to[tot]=b;
}
inline void oula(){
    st[++top]=1;
    while(top>0){
        int u=st[top],i=head[u];
        if(i){
            st[++top]=to[i];
            head[u]=nxt[i];
        }
        else --top,ans[++sum]=u;
    }
}
int main(){
    n=read();m=read();
    for(int i=1;i<=m;++i){
        int a=read(),b=read();
        add(a,b);add(b,a);
    }
    oula();
    for(int i=sum;i>=1;--i)printf("%d\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/PPXppx/p/11575334.html