Door Man(POJ 1300)

题目链接

题目描述

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:

  • Always shut open doors behind you immediately after passing through
  • Never open a closed door
  • 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 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:

  • 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).
  • 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!
  • 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.

输出格式

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”.

输入样例

START 1 2
1
\n

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

END
ENDOFINPUT

输出样例

YES 1
NO
YES 10

分析

题目大意是给定起点房间号m和n个房间,接下来n行每行的数字k代表第i号房间到第k号房间有一扇开着的门可以互通(即存在边),要求管家从m号房间出发到达0号房间并关闭所有开着的门,关上的门无法再打开通过,即所有门只能通过一次。显然,这是一个求欧拉路的问题,分别从欧拉通路和回路进行分析:

  • 欧拉通路:若存在欧拉通路,则为了满足题目要求起点和终点必须为m和0(m!=0),因为只有这样才能从m出发有且仅遍历所有边一次最终到达0号房间。
  • 欧拉回路:若存在欧拉回路,则为了满足题目要求m必须为0,因为欧拉回路是从m出发最终回到m的。

对于欧拉路中关闭的总门数,实际上就是图中的总边数(总边数=总度数÷2)。
需要注意的是,题目中可能存在非联通图如输入样例2,即存在孤立点。而题目中只是要求关闭所有开着的门,因此对于这些孤立点可以不考虑在图内。

欧拉通路/回路的判定

有向图

  • 欧拉通路:图是连通的,图中只有两个奇度点,分别是欧拉通路的两个端点。对于欧拉通路,除起点、终点外,每个点如果进入,显然一定要出去,因此都是偶点。
  • 欧拉回路:图是连通的,点均为偶度点。对于欧拉回路,每个点进入、出去的次数相等,因此没有奇点。

无向图

  • 欧拉通路:图是连通的,除两顶点外其余点的入度等于出度,且这两个顶点中,一个顶点入度比出度大1(起点),另一个入度比出度小1(终点)
  • 欧拉回路:图是连通的,图中所有点的入度等于出度。

源程序

//#include <bits/stdc++.h>	//poj不给用万能头
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#define MAXN 105
using namespace std;
int n,m,degree[MAXN],father[MAXN];
char str[MAXN];
int find(int x)	//并查集 
{
	if(x==father[x])return x;
	return father[x]=find(father[x]);	//路径压缩 
} 
void Union(int x,int y)	//合并 
{
	x=find(x);
	y=find(y); 
	if(x!=y)father[x]=y;
}
int main()
{
	while(scanf("%s",str)&&str[0]=='S'){
		scanf("%d%d",&m,&n);
		getchar(); 
		for(int i=0;i<n;i++)degree[i]=0,father[i]=i;	//初始化 
		int sum=0;	//统计总度数,边数=总度数/2 
		for(int i=0;i<n;i++){
			gets(str);
			int len=strlen(str);
			if(len==0)continue;
			int tmp=0;
			for(int j=0;j<=len;j++){
				if(str[j]<'0'||str[j]>'9'){
					Union(i,tmp);
					sum+=2;
					degree[i]++;
					degree[tmp]++;
					tmp=0;
				}
				else tmp=tmp*10+str[j]-'0';
			} 
		}
		gets(str);
		if(find(0)!=find(m)){	//0号与m号不连通 
			cout<<"NO"<<endl;
			continue;
		}
		int cnt=0;	//记录连通分量
		for(int i=0;i<n;i++)
            if(degree[i]&&i==find(i))	//度为0的点不计入图中,因为任何与它相通的房间门都是关的 
                cnt++;
		if(cnt>1){
			cout<<"NO"<<endl;
			continue;
		} 
		int num=0;	//统计奇度点数
		for(int i=0;i<n;i++)
			if(degree[i]&1)num++;
		if(num==0&&m==0)cout<<"YES "<<sum/2<<endl;	//欧拉回路:必须从0号房出发
		else if(num==2&&m!=0&&degree[0]%2==1&&degree[m]%2==1)	//欧拉通路:0点和起点分别为终点和起点 
			cout<<"YES "<<sum/2<<endl;
		else 	//多笔画问题 
			cout<<"NO"<<endl;
	}
	return 0; 
}
发布了19 篇原创文章 · 获赞 0 · 访问量 137

猜你喜欢

转载自blog.csdn.net/weixin_43960284/article/details/105185513
今日推荐