HDU 1878 欧拉回路

欧拉回路
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
 

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



#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;

const int MAXN=1000+5;

int DU[MAXN];
int n,m,sz;
int father[MAXN];

void edge_init(){
   memset(DU,0,sizeof(DU));
   for(int i=1;i<=n;i++){
       father[i]=i;
   }
}


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

int main()
{
    //FIN
    while(~scanf("%d",&n),n)
    {
        scanf("%d",&m);
        edge_init();
        int sum=n;
        for(int i=1;i<=m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            DU[u]++;
            DU[v]++;
            int root1=Find(u);
            int root2=Find(v);
            if(root1!=root2){
                father[root2]=root1;
                sum--;
            }

        }
        if(sum!=1){
            printf("0
");
            continue;
        }

        bool flag=1;
        for(int i=1;i<=n;i++){
            if(DU[i]%2==1){
                flag=0;
                printf("0
");
                break;
            }
        }
        if(flag)  printf("1
");


    }
    return 0;
}

  




原文地址:https://www.cnblogs.com/Hyouka/p/5743173.html