码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1878 欧拉回路

时间:2014-08-12 10:24:03      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   io   for   ar   div   

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;
}


HDU 1878 欧拉回路,布布扣,bubuko.com

HDU 1878 欧拉回路

标签:des   style   color   os   io   for   ar   div   

原文地址:http://blog.csdn.net/u013582254/article/details/38510785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!