[Template] Euler

Euler Road

Given an undirected graph, each edge just after seeking a path exactly once.

If all the points degrees are even, the presence of Euler; if there are only two points and the degree is odd, there are two points to this as a starting point, the end of the road Euler.

Eulerian path is a connected graph, the path can be resolved into a point number of disjoint rings +.

Euler Road

Direct search, then the stack is the reverse sequence of Euler Road.

Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define rep(i,l,r) for(register int i=(l);i<=(r);++i)
#define repdo(i,l,r) for(register int i=(l);i>=(r);--i)
#define il inline
typedef double db;
typedef long long ll;

//---------------------------------------
const int nsz=550,msz=1050;
int n,m;
int deg[nsz];
multiset<int> edge[nsz];
void adde(int f,int t){edge[f].insert(t);edge[t].insert(f);}
void erase(int f,int t){edge[f].erase(edge[f].find(t));}

int ans[nsz],pa=0;
void dfs(int p){
    for(multiset<int>::iterator i=edge[p].begin();i!=edge[p].end();i=edge[p].begin()){
        int v=*i;
        edge[p].erase(i);
        edge[v].erase(edge[v].find(p));
        dfs(v);
    }
    ans[++pa]=p;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>m;
    int a,b;
    rep(i,1,m)cin>>a>>b,++deg[a],++deg[b],adde(a,b),n=max(n,max(a,b));
    int fr=-1;
    rep(i,1,n){
        if(deg[i]&&fr==-1)fr=i;
        if(deg[i]&1){fr=i;break;}
    }
    dfs(fr);
    repdo(i,pa,1)cout<<ans[i]<<'\n';
    return 0;
}

Guess you like

Origin www.cnblogs.com/ubospica/p/11163496.html