Hdu 1878 欧拉回路[判断是否存在欧拉回路]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878

题目的意思很简单,就是给你一个无向图。。问存在欧拉回路吗?Yes or No。1000个节点。。。

看欧拉回路的定义。。

通过图中所有边一次且仅一次行遍所有顶点是回路,称为欧拉回路。。

具有欧拉回路的图称为欧拉图。。

解这个问题,非常的简单,就需要一个定理就好了。

无向图是欧拉图当且仅当图是连通图且没有奇度顶点。。。

那么,我们就仅仅需要判断这个图的连通性和顶点的度数就好啦。。。

Code:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;

const int N = 1e3 + 5;
int n, m, d[N], father[N];
void Init()
{
    for(int i = 1; i <= n; i ++){
        father[i] = i; d[i] = 0;
    }
}

int find(int x)
{
    if(x == father[x]) return x;
    return father[x] = find(father[x]);
}

void Union(int x, int y)
{
    int a = find(x), b= find(y);
    if(a != b) father[x] = b;
}

bool solve()
{
    int cnt = 0;
    for(int i = 1; i <= n; i ++){
        if(father[i] == i) cnt ++;
    }
    if(cnt != 1) return false;
    for(int i = 1; i <= n; i++){
        if(d[i] % 2) return false;
    }
    return true;
}

int main()
{
//    freopen("1.txt", "r", stdin);
    while(scanf("%d", &n) && n){
        scanf("%d", &m);
        Init();
        int x, y;
        for(int i = 0; i < m; i ++){
            scanf("%d %d", &x, &y);
            Union(x, y);
            d[x] ++; d[y] ++;
        }
        if(solve()) puts("1");
        else puts("0");
    }
    return 0;
}

慢慢的来弥补图论上的缺陷吧。。come on!! 加油。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值