zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    一键登录功能实现
    weblogic.xml中的虚拟目录的配置
    oracle定时器在项目中的应用
    redis主从架构的搭建
    oracle数据库中的trim不起作用
    datetimepicker日期框选择后,无法触发bootstrapValidator
    付出总有收获
    失落
    开启产品之路
    Nodejs入门级
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4279301.html
Copyright © 2011-2022 走看看