POJ1300 Door Man 无向图欧拉通路

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
判断图中是否存在欧拉回路,终点规定为0,起点指定。
图为无向图,所以判断方法是如果不存在度数为奇数的节点(房间),并且起点为0则输出YES X。如果存在2个奇数度数的节点,并且分别为0 和 S(S!=0)则存在欧拉回路。其他情况都不符合规定。
这道题比较简单,就是输入要注意使用cin.getline()。
Door Man
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1447 Accepted: 539

Description

You are a butler in a large mansion. This mansion has so many rooms that they are merely referred to by number (room 0, 1, 2, 3, etc...). Your master is a particularly absent-minded lout and continually leaves doors open throughout a particular floor of the house. Over the years, you have mastered the art of traveling in a single path through the sloppy rooms and closing the doors behind you. Your biggest problem is determining whether it is possible to find a path through the sloppy rooms where you:

  1. Always shut open doors behind you immediately after passing through
  2. Never open a closed door
  3. End up in your chambers (room 0) with all doors closed

In this problem, you are given a list of rooms and open doors between them (along with a starting room). It is not needed to determine a route, only if one is possible.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 3 components:

  1. Start line - A single line, "START M N", where M indicates the butler's starting room, and N indicates the number of rooms in the house (1 <= N <= 20).
  2. Room list - A series of N lines. Each line lists, for a single room, every open door that leads to a room of higher number. For example, if room 3 had open doors to rooms 1, 5, and 7, the line for room 3 would read "5 7". The first line in the list represents room 0. The second line represents room 1, and so on until the last line, which represents room (N - 1). It is possible for lines to be empty (in particular, the last line will always be empty since it is the highest numbered room). On each line, the adjacent rooms are always listed in ascending order. It is possible for rooms to be connected by multiple doors!
  3. End line - A single line, "END"

Following the final data set will be a single line, "ENDOFINPUT".

Note that there will be no more than 100 doors in any single data set.

Output

For each data set, there will be exactly one line of output. If it is possible for the butler (by following the rules in the introduction) to walk into his chambers and close the final open door behind him, print a line "YES X", where X is the number of doors he closed. Otherwise, print "NO".

Sample Input

START 1 2
1

END
START 0 5
1 2 2 3 3 4 4




END
START 0 10
1 9
2
3
4
5
6
7
8
9

END
ENDOFINPUT

Sample Output

YES 1
NO
YES 10
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>

using namespace std;

#define MAXN 100

char str[50];
int counts[MAXN];
int s,n;
int a,b;
int len;

void init()
{
	memset(counts,0,sizeof(counts));
}

void solve()
{
	char ch[255];
	len=0;
	int p=0;
	int temp;
	for(int i=0;i<n;i++)
	{
		cin.getline(ch,255);
		p=0;
		while(sscanf(ch+p,"%d",&temp)==1)
		{
			len++;
			counts[temp]++;
			counts[i]++;
			while(ch[p]!=0 && ch[p]==' ')	p++;
			while(ch[p]!=0 && ch[p]!=' ') p++;
		}
	}
	cin.getline(str,50);
	a=b=0;
	for(int i=0;i<n;i++)
	{
		if(counts[i]%2==0)
			a++;
		else
			b++;
	}
	if(b==0 && s==0)
		cout<<"YES "<<len<<endl;
	else
		if(counts[s]%2==1 && counts[0]%2==1 && b==2 && s!=0)
			cout<<"YES "<<len<<endl;
		else
			cout<<"NO"<<endl;
}

int main()
{
	char ch[255],tp[20];
	while(1)
	{
		cin.getline(ch,255);
		if(ch[0]=='S')
		{
			sscanf(ch,"%s%d%d",tp,&s,&n);
			init();
			solve();
		}
		else
			break;
		
	}
	return 0;
} 

POJ 1300 Door Man无向图欧拉定理、gets、sscanf 立即下载

相关推荐

POJ-1300(汉密尔顿回路)

题目:http://poj.org/problem?id=1300 分析:简单的汉密尔顿回路问题,需要注意的是要回到node 0,因此需要判断在有两个奇度node时,start node必须是非0的那个,没有判断连通性,1A,看了discuss之后,发现竟然“判断连通性会WA,不判断就AC”,也是醉了 #include #include #include #include us

还有多远 707

POJ1300 Door Man

无向图欧拉回路判定。

H992109898的博客 497

POJ 1300 判断是否存在欧拉回路(包含定理)

题目描述: 你是一座大庄园的管家。庄园有很多房间,编号为0、1、2、3,...。你的主人是一个心不在 焉的人,经常沿着走廊随意地把房间的门打开。多年来,你掌握了一个诀窍:沿着一个通道,穿 过这些大房间,并把房门关上。你的问题是能否找到一条路径经过所有开着门的房间,并使得: 1) 通过门后立即把门关上; 2) 关上了的门不再打开; 3) 最后回到你自己的房间(房间0),并且所有的门都已经

liwei的专栏 2582

POJ 1300 欧拉通路&欧拉回路

系统的学习一遍图论! 先介绍一些概念。 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路。 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路。 具有欧拉回路的无向图G称为欧拉图。 有向图: D为基图连通的有向图,则称经过D的每一条边并且仅一次的路径为有向欧拉通路。 如果该通路是回路,则称为有向欧拉回路。 具有有向欧拉回路的有向图D称为有

just_water 1419

No.17 POJ1300 判断欧拉回路

欧拉回路: 两种情况 所有点度为偶数,且出发点一定是终止点 有两个奇数度点,分别为出发点和终止点 // ShellDawn // POJ1300 // No.17 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define MM(x) me...

ShellDawn的博客 239

POJ--1300--Door Man【判断无向图欧拉通路

链接:http://poj.org/problem?id=1300 题意:有n个房间,每个房间有若干个门和别的房间相连,管家从m房间开始走,要回到自己的住处(0),问是否有一条路可以走遍所有的门并且没有重复的路。 思路:判断是否存在欧拉通路,根据欧拉通路欧拉回路的性质来做。有两种情况:一种是欧拉回路,所有房间的门的个数都是偶数个,并且此时初始房间不是0,此时存在要求的路径,如果初始是

阳光照耀圣西罗 1193

POJ--1300--Door Man【推断无向图欧拉通路

链接:http://poj.org/problem?id=1300 题意:有n个房间。每一个房间有若干个门和别的房间相连。管家从m房间開始走。要回到自己的住处(0),问是否有一条路能够走遍全部的门而且没有反复的路。 无向图欧拉通路充要条件:G为连通图,而且G仅有两个奇度结点(度数为奇数的顶点)或者无奇度结点。 无向图欧拉回路充要条件:G为无奇度结点的连通图。 思路:推断是否...

weixin_30797027的博客 123

POJ 1300 Door Man (无向图欧拉通路和回路的判定+并查集)

Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3284   Accepted: 1368 Descriptio...

pxlsdz的博客 526

POJ 1300 Door Man欧拉通路

题目描述:  你是一座大庄园的管家。庄园有很多房间,编号为 0、1、2、3,...。你的主人是一个心不在 焉的人,经常沿着走廊随意地把房间的门打开。多年来,你掌握了一个诀窍:沿着一个通道,穿 过这些大房间,并把房门关上。你的问题是能否找到一条路径经过所有开着门的房间,并使得: 1) 通过门后立即把门关上; 2) 关上了的门不再打开; 3) 后回到你自己的房间(房间 0),并且所有的门都已经关闭了

Deng's site \# 987

POJ-1300 Door Man

题目大意: 你是一座大庄园的管家,庄园有很多房间,编号从0-n。你需要从m个房间走回到0号房间,并且通过一些房间,在通过这些房间的时候立即把们关上,关上了的门也不能再打开,并且在你回到0号房间的时候,所有的门已经关闭了。问你是否存在这样的路; 解题思路: 这题是道很简单的欧拉通路/欧拉回路判断 我们知道一个定理:无向图G存在欧拉通路的充要条件是:G为联通图,并且G仅有两个奇度数节点或者无奇

Wilbert的博客 488

POJ 1300 Door Man(欧拉回路判定)

POJ 1300 Door Man(欧拉回路判定) http://poj.org/problem?id=1300 题意:给你N个房间(图节点)以及房间之间的门(图的边),且给你初始的房间号M,问你从初始房间走,可不可以经过每个门仅1次,最后到达0号房间.且所有的门都被你走过1次? 分析:仔细一看,其实这个问题就是判定是否存在无向图欧拉回路或通路的问题.本题要理清所有可能的情况,理清了之后就

code 1364

POJ1300 Door Man 欧拉回路的判断

题目链接: 1300 题意: 一个房子中有(编号0~N-1)N个房间和X个连通两个房间的门,每次经过一扇门时这扇门会被关闭。问:一个人从M号房间出发能否成功到达0号房间并关闭所有门。 题解: 此题是欧拉回路的入门题,首先学习无向图欧拉回路的判断定理: 无向图G 存在欧拉通路的充要条件是:G 为连通图,并且G 仅有两个奇度结点(度数为奇数的

旧时光 1349

poj1300 Door Man(欧拉回路判定)

题意:给你N个房间以及房间之间的门,且给你初始的房间号M,问你从初始房间走,可不可以经过每个门仅1次,最后到达0号房间.且所有的门都被你走过1次? 思路:将房间当成顶点,门作为边,转化为判断无向图是否存在欧拉回路或者通路。          所有门都被走一遍,所以任何有门的房间都必须是在同一连通分量:即degree不为0的节点所属并查集唯一.(因为允许存在孤立的一点,房间)    

围巾的ACM博客 554

POJ-1300 Door Man 欧拉回路判定

题意:给定一个无向图的关系,判定是否存在一条从M点出发回到0点并且走遍所有边的通路,也即欧拉通路。 解法:该题如果当出发点就为0点话就等效于是否存在欧拉回路了。欧拉通路的判定条件为:连通的无向图中,度为奇数节点的个数为0个或者是2个。由于该题限定了起点和端点,因此度为奇数的点只能够由两个,且为M和0。当M==0时,奇数节点个数为0个符合题意,此时将构成一条欧拉回路。 代码如下: #inc...

weixin_34279061的博客 121

zoj 1395 && poj 1300 Door Man

题意:从自己的房间出发经过所有的房间,最后回到0号房间。通过后的门会立即关上,关上的门不再打开。 分析:首先根据给定的条件建图,若从0号房间出发,那么我们便是判断欧拉回路是否存在;若从其他房间出发,那么我们就是判断是否存在欧拉通路无向图欧拉回路判定:一个图为无奇度结点的联通图。 无向图欧拉通路的判定:图为联通图,并且仅有两个奇度节点,而且欧拉通路必以这两个结点为端点。

mxc的专栏 538
上一篇: POJ 3159 Candies 差分约束
下一篇: POJ1273 Drainage Ditches 网络最大流
Albafica
博客等级 码龄15年 98粉丝 359原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值