• HDU 1878 欧拉回路


    Problem Description
    欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且能够回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
     

    Input
    測试输入包括若干測试用例。每一个測试用例的第1行给出两个正整数,各自是节点数N ( 1 < N < 1000 )和边数M;随后的M行相应M条边,每行给出一对正整数,各自是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
    束。
     

    Output
    每一个測试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
     

    Sample Input
    3 3 1 2 1 3 2 3 3 2 1 2 2 3 0
     

    Sample Output
    1 0
     

    Author
    ZJU
     
    解决无向图的欧拉回路就是直接上。写了UVA的那道,这题是弱化版。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    using namespace std;
    const int maxn=1100;
    int d[maxn];
    int pre[maxn];
    int n,m;
    void init()
    {
        memset(d,0,sizeof(d));
        for(int i=0;i<=n;i++)
            pre[i]=i;
    }
    int find_root(int x)
    {
        if(x!=pre[x])
            pre[x]=find_root(pre[x]);
        return pre[x];
    }
    bool ok()
    {
        int x=-1;
        for(int i=0;i<=n;i++)
        {
            if(d[i]&1)
                return false;;
            if(d[i])
            {
                if(x==-1)
                    x=find_root(i);
                else
                {
                    if(x!=find_root(i))
                        return false;
                }
            }
        }
        return true;
    }
    int main()
    {
        int x,y;
        while(~scanf("%d",&n))
        {
            if(n==0)
                break;
            scanf("%d",&m);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&x,&y);
                d[x]++;
                d[y]++;;
                pre[find_root(x)]=find_root(y);
            }
            if(!ok())
                cout<<0<<endl;
            else
                cout<<1<<endl;
        }
        return 0;
    }
    


  • 相关阅读:
    QT导出csv
    QTableView右键删除功能实现
    搜索框实现
    centos同步时间报错问题解决
    git怎么撤回提交
    python2报错 ascii' codec can't decode byte 0xe4 in position 5: ordinal not in range python3没问题
    使用poython将文本转换成doc文档
    CSS层叠样式总结
    Jrebel热部署插件安装
    VMware为CentOS设置固定ip
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4279301.html
Copyright © 2020-2023  润新知