hdu1878欧拉回路

怎么说呢,做完这题发现自己的代码好挫好挫啊。。。

判断无向图欧拉回路,只需要判断连通与无奇点就行,待会转一个欧拉回路的总结。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1010;
int degree[N];
int arr[N];
int p[N];
int find(int x)
{
    if(x!=p[x])
    p[x]=find(p[x]);
    return p[x];
}
void unions(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        p[fy]=p[fx];
    }
}

int main()
{
    int n,m,flag,x,y;
    while(scanf("%d",&n)!=EOF&&n)
    {

        scanf("%d",&m);
        flag=1;
        for(int i=1;i<=n;i++)
        {
            p[i]=i;
            arr[i]=0;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            arr[x]++;
            arr[y]++;
            unions(x,y);
        }
        for(int i=1;i<=n;i++ )
        {
            if(p[i]!=1)
            {
                flag=0;
                break;
            }
            else if(arr[i]%2!=0)
            {
                flag=0;
                break;
            }
        }
        if(flag)
        cout<<1<<endl;
        else
        cout<<0<<endl;
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/amourjun/p/5134165.html