• 模板|欧拉路径与欧拉回路


    1.判断无向图的欧拉回路和欧拉路径

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int MAX_N = 100;
    const int MAX_M = 10000;
    struct edge {
        int v, next;
        int len;
    } E[MAX_M];
    int p[MAX_N], eid;
    void init() {
        memset(p, -1, sizeof(p));
        eid = 0;
    }
    void insert(int u, int v) {
        E[eid].v = v;
        E[eid].next = p[u];
        p[u] = eid++;
    }
    int n,m;
    int degree[MAX_N];
    int cnt;
    bool vis[MAX_N];
    void dfs(int u){
        vis[u] = true;
        cnt++;
        for(int i=p[u];i!=-1;i=E[i].next){
            int v = E[i].v;
            if(!vis[v]){
                dfs(v);
            }
        }
    }
    
    void euler(){
        dfs(1);
        if(cnt!=n){
    		cout<<"It doesn't have an euler path!"<<endl;
            return;
        }
        int cntodd = 0;
        for(int i=1;i<=n;i++){
    		if(degree[i]%2 == 1){
                cntodd++;
            }
        }
        if(cntodd == 0){
    		cout<<"It has an euler circuit!"<<endl;
        }else if(cntodd == 2){
            cout<<"It has an euler path!"<<endl;
        }else{
    		cout<<"It doesn't have an euler path!"<<endl;
        }
    }
    
    
    int main() {
        init();
        memset(degree,0,sizeof(degree));
        cin>>n>>m;
        for(int i=0;i<m;i++){
            int u,v;
            cin>>u>>v;
            insert(u,v);
            insert(v,u);
            degree[u]++;
            degree[v]++;
        }
        euler();
        return 0;
    }
    

    2.判断无向图的欧拉回路和欧拉路径

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int MAX_N = 100;
    const int MAX_M = 10000;
    struct edge {
        int v, next;
        int len;
    } E[MAX_M];
    int p[MAX_N], eid;
    void init() {
        memset(p, -1, sizeof(p));
        eid = 0;
    }
    void insert(int u, int v) {
        E[eid].v = v;
        E[eid].next = p[u];
        p[u] = eid++;
    }
    
    int n,m;
    int degree[MAX_N];
    
    int euler(){
        int first = 0,last = 0;
        for(int i = 1;i <= n;i++){
            if(degree[i]<-1 || degree[i] > 1){
                cout<<"It doesn't have an euler path!"<<endl;
                return 0;
            }else if(degree[i] == -1){
                if(first != 0){
                    cout<<"It doesn't have an euler path!"<<endl;
                    return 0;
                }else{
                    first = i;
                }
            }else if(degree[i] == 1){
    			if(last != 0 ){
                    cout<<"It doesn't have an euler path!"<<endl;
                    return 0;
                }else{
                    last = i;
                }
            }
        }
        if(first == 0 && last == 0){
            cout<<"It has an eluer circuit!"<<endl;
            return 1;
        }else if(first != 0 && last != 0){
            cout<<"It has an euler path!"<<endl;
            return first;
        }
    }
    
    int main() {
        init();
        memset(degree,0,sizeof(degree));
        cin>>n>>m;
        for(int i=0;i<m;i++){
            int u,v;
            cin>>u>>v;
            insert(u,v);
            degree[u]--;
            degree[v]++;
        }
        euler();
        return 0;
    }
    

    3.找出有向图的欧拉回路

    #include <iostream>
    #include <cstring>
    #include <stack>
    using namespace std;
    const int MAX_N = 100;
    const int MAX_M = 10000;
    struct edge {
        int v, next;
        int len;
    } E[MAX_M];
    int p[MAX_N], eid;
    void init() {
        memset(p, -1, sizeof(p));
        eid = 0;
    }
    void insert(int u, int v) {
        E[eid].v = v;
        E[eid].next = p[u];
        p[u] = eid++;
    }
    int n, m;
    int degree[MAX_N];
    int euler() {
        int first = 0, last = 0;
        for (int i = 1; i <= n; i++) {
            if (degree[i] < -1 || degree[i] > 1) {
                return 0;
            } else if (degree[i] == -1) {
                if (first != 0) {
                    return 0;
                } else {
                    first = i;
                }
            } else if (degree[i] == 1) {
                if (last != 0) {
                    return 0;
                } else {
                    last = i;
                }
            }
        }
        if (first == 0 && last == 0) {
            return 1;
        } else if (first != 0 && last != 0) {
            return first;
        } else {
            return 0;
        }
    }
    
    stack<int> path;
    void dfs(int u){
    	while(p[u] != -1){
            int v = E[p[u]].v;
            p[u] = E[p[u]].next;//删除这条边就是把p[u]赋值位E[p[u]].next
            dfs(v);
        }
        path.push(u);
    }
    
    bool euler_find(){
        int start;
        start = euler();
        if(start == 0){
            return false;
        }else{
            dfs(start);
            return path.size() == m + 1;
        }
    }
    
    int main() {
        init();
        memset(degree,0,sizeof(degree));
        cin>>n>>m;
        for(int i=0;i<m;i++){
            int u,v;
            cin>>u>>v;
            insert(u,v);
            degree[u]--;
            degree[v]++;
    	}
        if(!euler_find()){
            cout<<"It doesn't have an euler path!"<<endl;
        }else{
            while(!path.empty()){
                cout<<path.top()<<endl;
                path.pop();
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Git原理与命令大全
    【网络安全】加解密算法最详解
    陪你阅读《区块链:从数字货币到信用社会》序一
    Splunk初识
    红帽学习记录[RHCE] ISCSI远程块储存
    DNS 域名系统与邮件服务器
    红帽学习记录[RHCE] 防火墙与网络合作
    红帽学习笔记[RHCE]网络配置与路由转发
    红帽学习笔记[RHCE]OpenLDAP 服务端与客户端配置
    红帽学习笔记[RHCSA] 第二周
  • 原文地址:https://www.cnblogs.com/fisherss/p/10683867.html
Copyright © 2020-2023  润新知