By Male (POJ 1300)

Topic Link

Title 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:

  • 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 Format

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.

Output Format

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
\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

Sample Output

YES 1
NO
YES 10

analysis

Subject to the effect that a given starting room number and m n number of rooms, then the row number n k represents the i-th row of each room number to the k room has an open door can communicate (i.e., there is an edge), the requirements from the Butler m number of rooms departure and arrival 0 room and close all the open door, closed door can not be opened again to pass, that all the doors can only be once. Obviously, this is a way of Euler problems were analyzed from Euler paths and circuits:

  • Euler path: If there is Euler path, in order to meet the subject requirements must start and end points m and 0 (m = 0!), Because the only way to have a departure from all sides m and traversed only once final numbers 0 room.
  • Euler: Euler circuit if there is to meet the subject requirements m must be zero, because Euler tour starting from the back of m m final.

The total number of gates for the Euler path closed, in fact the total number of edges in the graph (total = total number of edges degrees ÷ 2).
Note that, the subject may be present in the non-connected graph as input sample 2, i.e. the presence of outlier. The title only requires close all open doors, and therefore these points can not be considered in isolation in FIG.

Euler path / decision circuit

Directed graph

  • Euler passage: FIG communication is, FIG singularity of only two points, two endpoints are Euler path. For Euler path, in addition to the start and end points, each point if they go into, obviously you must go out, and therefore are even point.
  • Euler: FIG communication is, even points are of degree. For Euler, enter each point is equal to the number of times out, so there is no singular point.

Undirected graph

  • Euler passage: FIG communication is, in addition to the two vertices of the remaining points is equal to a degree, and the two vertices, one vertex of the big one (the starting point), the other is smaller than the ratio of the degree of 1 (end)
  • Euler: graph is connected, the figures for all points equal to the degree.

Source

//#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; 
}
Published 19 original articles · won praise 0 · Views 137

Guess you like

Origin blog.csdn.net/weixin_43960284/article/details/105185513