zoukankan      html  css  js  c++  java
  • 欧拉回路

     

    求欧拉回路dfs代码:

    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    int n,m,dis[101],dis_=0,root=0x7fffffff,path[10001],now=0;
    
    bool map[101][101],ok=false,if_[101];
    
    void euler(int now,int gra)
    {
        path[gra]=now;
        if(gra==m+1)
        {
            ok=true;
            return ;
        }
        for(int i=1;i<=n;i++)
        {
            if(map[now][i])
            {
                map[now][i]=false;
                map[i][now]=false;
                euler(i,gra+1);
                if(ok) return;
                map[now][i]=true;
                map[i][now]=true;
            }
        }
    }
    
    void euler_back(int now,int gra)
    {
        path[gra]=now;
        if(gra==m+1)
        {
            if(now==root) ok=true;
            return ;
        }
        for(int i=1;i<=n;i++)
        {
            if(map[now][i]&&!if_[i])
            {
                map[now][i]=false;
                map[i][now]=false;
                euler_back(i,gra+1);
                if(ok) return;
                map[i][now]=true;
                map[now][i]=true;
            }
        }
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        int from,to;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&from,&to);
            map[from][to]=true;
            map[to][from]=true;
            dis[from]++,dis[to]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(dis[i]%2)
            {
                root=min(i,root);
                dis_++;
            }
        }
        if(dis_==2)
        {
            if_[root]=true;
            euler(root,1);
            if(ok)
            {
                cout<<"true 1"<<endl;
                for(int i=1;i<=m;i++) cout<<path[i]<<' ';
            }
            else cout<<"false"<<endl;
            cout<<endl;
        }
        else
        {
            if(!dis_)
            {
                euler_back(1,1);
                if(ok)
                {
                    cout<<"true 2"<<endl;
                    for(int i=1;i<=m;i++) cout<<path[i]<<" ";
                }
                else cout<<"false";
                cout<<endl;
            }
            else
            {
                cout<<"false"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    jquery内容过滤器
    jquery的each()
    jquery表单过滤器
    jquery评分星星
    UVa 1595 Symmetry (set && math)
    UVa 1592 Database (map)
    Codeforces 886E Maximum Element 组合数学 + dp
    Codeforces 725E Too Much Money (看题解)
    可持久化字典树
    Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6286710.html
Copyright © 2011-2022 走看看