Door Man
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2596   Accepted: 1046

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

题意:有很多房间,编号为0,1,2,3,...。输入m,n。m为起始房间号;n为房间总数(1≤n≤20)。接下来n行列出了房间通向其他的房间号。(注意:空行也是输入,表示这个房间通向其他的房间号都已经出现了。)从m开始通过所有的门,通过后把门关上,关上了的门不能打开,最后回到0。

思路:最基础的欧拉通路的判断,判断是否是起点是m,终点是0的欧拉通路(默认是连通图)。主要要注意输入输出的格式。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int door[];
char s[];
int Init()
{
int len=;
char ch;
while((ch=getchar())&&ch!='\n')
{
s[len++]=ch;
}
return len;
}
int main()
{
int i,j,m,n;
int len;
int ans=;
while(scanf("%s",s)!=EOF)
{
len=strlen(s);
if(len==) break;
scanf("%d%d",&m,&n);
getchar();
memset(door,,sizeof(door));
ans=;
for(i=; i<n; i++)
{
len=Init();
for(j=; j<len; j++)
{
int num=;
while(j<len&&s[j]!=' ')
{
num=num*+(s[j]-'');
j++;
}
ans++;
door[i]++;
door[num]++;
}
}
scanf("%s",s);
int even=,odd=;
for(i=; i<n; i++)
{
if(door[i]%==) even++;
else odd++;
}
if(odd==&&m==) cout<<"YES "<<ans<<endl;
else if(odd==&&m!=&&door[]%!=&&door[m]%!=) cout<<"YES "<<ans<<endl;
else cout<<"NO"<<endl;
}
return ;
}

POJ 1300.Door Man 欧拉通路的更多相关文章

  1. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  2. POJ 1300 欧拉通路&欧拉回路

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

  3. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  4. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  5. POJ 1386 Play on Words(有向欧拉通路 连通图)

    题意  见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边  然后就是输入m条边  推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和 ...

  6. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  7. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  8. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

随机推荐

  1. c# 制作自定义控件并生成dll文件旷展到其他项目使用

    1.新建项目,同时添加一个类库,然后在类库里面定义自己的控件:完毕生成项目,然后在类库的bin目录下就自动生成了这个自定义控件的dll文件. 2.在需要使用这个自定义控件的项目里,直接ctrl+c,c ...

  2. 《图像处理实例》 之 目标旋转矫正(基于区域提取、DFT变换)

    目标:1.把矩形旋转正.          2.把文字旋转校正.                                                                     ...

  3. svn+apache+ssl快速部署

    在svn+apache文章中已经成功搭建了web-svn,由于在http网络上数据都是以明文传输,公司的源码需要一定的保密机制,基于安全考虑现整合web-svn+ssl.构建安全的svn服务器, 1. ...

  4. sql server 定期自动清理日志

    https://blog.csdn.net/dqs78833488/article/details/51372491

  5. HTML5 ES6 语法基础

    // 解构赋值 let [a, b, c, [s,e],d] = ["aa", "bb", "cc", [12, 23], "dd ...

  6. HTML5 Canvas ( 绘制一轮弯月, 星空中的弯月 )

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. JS转义解码方法

    之前只知道可以解决传值乱码问题,今天刚好看到,从新补充下: JavaScript中有三个可以对字符串编码的函数,分别是: 转义方法: escape();//函数可对字符串进行编码 encodeURI( ...

  8. 解决打开visio2013提示windows正在配置的问题

    由于之前装过office2007.也装过2010版本.新安装visio2013就会出现如下情况 解决办法: 主要是要清理完visio2010及之前的那些没用选项 1.在cmd命令下打开regedit注 ...

  9. chrome 常用插件下载安装

    可在google的应用商店进行下载:chrome://apps/ 但大多时间无法链接. 国内插件下载地址: http://www.cnplugins.com http://chromecj.com/ ...

  10. libcurl 支持openssl 但不能访问https

    重新编译了libcurl 去访问https 地址还是不能访问 从网上找到了解决方案: curl有两种方式使用https : 1. 设定为不验证证书和HOST code = curl_easy_seto ...