POJ 1300 Door Man(欧拉回路_格式控制*)

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

貌似主要考的是输入输出……

由于在START M N后面回车的处理不当导致WA*N,反正貌似scanf("%d%d\n",&a,&b)之流不管用。还是直接getchar()好。。

欧拉回路小题

1.判断所有边的度是否为偶数,并且起始点在0(因为最后要回到0点),则有欧拉回路

2.如果起始点m和0点的度数为奇数,并且m不为0,则有欧拉通路

get新技能,新的一种输入方式。下面附上代码\\

#include<iostream>
#include<cstring>
#include<sstream>
#include<cstdio>
using namespace std;
int main()
{
	string buf;
	stringstream stream;
	int i,j,n,m;
	int deg[30];
	cin>>buf;
	while(buf!="ENDOFINPUT") {
		int side=0;
		memset(deg,0,sizeof(deg));
		cin>>m>>n;
		getchar();
		for(i=0;i<n;i++) {
			int tem;
			getline(cin,buf);
			stream<<buf;
			while(stream>>tem) {
				side++;
				deg[i]++;
				deg[tem]++;
			}
			stream.clear();
		}
		cin>>buf;
		int even=0;
		int odd=0;
		for(i=0;i<n;i++) {
			if(deg[i]%2) odd++;
			else even++;
		}
		if(odd==0 && m==0) printf("YES %d\n",side);
		else if(odd==2 && deg[0]%2 && deg[m]%2 && m!=0) printf("YES %d\n",side);
		else printf("NO\n");
		 cin>>buf;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 11:46:15

POJ 1300 Door Man(欧拉回路_格式控制*)的相关文章

poj 1300 Door Man 欧拉回路

题目链接:http://poj.org/problem?id=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 door

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

题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉通路的问题了,但实际上,就只有两种情况能够输出YES,以房间为顶点,连接房间之间的门为边构造图,这两种情况分别是存在欧拉回路和欧拉通路的情况:所有房间都是偶数个门并且起始房间就是0,所以可以回到0,存在欧拉回路:有两个房间的门是奇数个,其余都是偶数个,这种情况下,要求出发房间和0房间的门是奇数个,并

POJ 1300 Door Man

判断是否欧拉回路. 很蛋疼的一道题,加上DFS判所有点是否连通就无限WA.(并查集也可判) 直接定理就AC了.都不知道所有点是不是在一个 连通块里面. 然后他们说:Your master is a particularly absent-minded lout and continually leaves doors open throughout a particular floor of the house. 这句话就表明了连通--问题是,中间关几个门让他无法通过,前后都有门没关怎么办--

POJ - 1780 Code (欧拉回路+手写DFS)

Description KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are severa

C++输出流的格式控制

inline SMANIP(long)     resetiosflags(long _l) { return SMANIP(long)(__resetiosflags, _l); }inline SMANIP(int)      setfill(int _m) {return SMANIP(int)(__setfill, _m); }inline SMANIP(long)     setiosflags(long _l) {return SMANIP(long)(__setiosflags,

【c++】c++格式控制输出简单应用

// c++格式控制输出简单应用 // 九九乘法表对齐 #include <iostream> #include <iomanip> using namespace std; int main() { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { //cout << i << "*" << j << "="

html标签,格式控制标签,内容容器标签,超链接标签,图片标签,表格

打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background  背景壁纸.图片 text  文字颜色 topmargin  上边距 leftmargin   左边距 rightmargin 右边距 bottommargin  下边距 body属性用法示例: 格式控制标签: <font color="" face="" size=""></font> 分别控制字体的颜

shell脚本编程(严格的终端格式控制,美丽的输出字体颜色)

#!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色30-37 echo -e "\033[30m黑色字\033[0m" echo -e "\033[31m红色字\033[0m" echo -e "\033[32m绿色字\033[0m" echo -e "\033[33m黄色字\033[0m" echo -e "\033[34m蓝色字\033[0m" echo -e "\033

c++IO之预定义格式控制

在C语言里,我们可以通过函数printf和scanf来进行格式化控制.而在C++中仍然包含了前者,但还提供了以下两种格式控制的方法:(1)使用流成员函数进行格式控制;(2)使用预定义操作符进行格式控制.下面我来一一介绍: 1.流成员函数主要是指ios类(流基类)中的,分别有: (1).设置状态标志流成员函数setf 一般格式:long ios::setf(long flags),调用格式:流对象.setf(ios::状态标志) ios类的状态标志有: 因为状态标志在ios类中定义为枚举值,所以在