九度:1027<欧拉回路><搜索>

// 浙大2008机试
// 
// 欧拉回路
// 充要条件:1.连通;2.每个点的度为偶数
// 连通:一次dfs后,所有的点都标记为已访问。
// 
// 

#include  <stdio.h>
#include  <cstring>
#include  <stdlib.h>
#include  <cctype>

#include  <iostream>
#include  <string>
#include  <algorithm>
#include  <map>
#include  <set>
#include  <vector>
#include  <stack>
#include  <queue>

#define SIZE 1005

using namespace std;

//--------------------
int graph[SIZE][SIZE];
int degree[SIZE];
bool visited[SIZE];
int m, n;
//--------------------

void Init()
{
	memset(graph, 0, sizeof(graph));
	memset(degree, 0, sizeof(degree));
	memset(degree, 0, sizeof(degree));
	m=0;
}

void Input()
{
	scanf("%d", &m);
	int i, x, y;
	for(i=0; i<m; i++)
	{
		scanf("%d%d", &x, &y);
		graph[x][y] = graph[y][x] = 1;
		degree[x]++;
		degree[y]++;
	}
}

void dfs(int x)
{
	visited[x] = 1;
	for(int i=1; i<=n; i++)
	{
		if(graph[x][i] == 1 && visited[i] == 0) //访问相连的并且没有访问的节点
		{
			dfs(i);
		}
	}
	return ;
}

bool Conn()
{
	for(int i=1; i<=n; i++)
	{
		if(visited[i] == 0)
			return false;
	}
	return true;
}

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


int main()
{

#ifdef ONLINE_JUDGE
#else
	freopen("E:\\in.txt", "r", stdin);
#endif

	while(scanf("%d", &n) && n)
	{
		Init();
		Input();
		dfs(1);
		if( Conn() && Oushu())
		{
			printf("1\n");
		}
		else
		{
			printf("0\n");
		}

	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值