HDU1878无向图判断欧拉回路


/**********************
* author:crazy_石头
* Pro:HDU1878
* algorithm: Euler Road/use dfs to print path
* 无向图判欧拉回路:若能dfs整个图即图是联通的还有每个顶点度数为偶数即可形成欧拉回路
* Judge Status:Accepted
* Memory:4184K
* Time:328ms
* date:2013/11/02
***********************/

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,h,n)  for(int i=(h);i<=(n);i++)
#define rrep(i,l,m) for(int i=(l);i>=(m);i--)

const int maxn=1000+5;

int map[maxn][maxn],d[maxn];
bool vis[maxn];
int n,m;

inline void dfs(int u)
{
    vis[u]=1;
    rep(i,1,n)
    {
        if(!vis[i]&&map[u][i])
        dfs(i);
    }
}//若联通则一遍dfs后所有顶点都被访问;

int main()
{
    int a,b,i,j;
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d",&m);
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        memset(d,0,sizeof(d));

        rep(i,1,m)
        {
            scanf("%d%d",&a,&b);
            map[a][b]=map[b][a]=1;
            d[a]++;
            d[b]++;
        }
        dfs(1);
        bool ok=0;
        rep(i,1,n)
        {
            if(!vis[i])
            {
                ok=1;
                break;
            }
        }
        rep(i,1,n)
        {
            if(d[i]&1)//判断顶点度数;
            {
                ok=1;
                break;
            }
        }
        if(ok)
            printf("0\n");
        else
            printf("1\n");
    }
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Default )

你可能感兴趣的:(HDU1878无向图判断欧拉回路)