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. psc格式的文件是什么

    psc格式的文件是什么 是navicat 这个工具导出的数据文件 可以使用备份还原功能  提取sql

  2. 尝试了一些时间,最简单的apache上设置用IP访问一个虚拟目录

    就是其实新建一个 多域名访问的设置 <VirtualHost *:80> DocumentRoot /var/www/ ServerName IP</VirtualHost> ...

  3. 20.Scrapy日常练手

    1.创建爬虫项目: scrapy  startproject tutorial 2.创建 spider cd tutorial scrapy  genspider quotes quotes.tosc ...

  4. Celery ---- 分布式队列神器 ---- 入门

    原文:http://python.jobbole.com/87238/ 参考:https://zhuanlan.zhihu.com/p/22304455 Celery 是什么? Celery 是一个由 ...

  5. UI5-文档-4.33-Routing Back and History

    现在我们可以导航到细节页面并显示发票,但是还不能回到概览页面.我们将向细节页面添加一个back按钮,并实现一个函数,再次显示概述页面. Preview A back button is now dis ...

  6. Asp.Net Core参考资料

    Every day up!!!!!! 1.Asp.Net官方文档 2.<Professional C# 6 and .NET Core 1.0> 翻译 3.ASP.NET Core中文文档 ...

  7. Java 使用getClass().getResourceAsStream()方法获取资源

    之前想获取一个资源文件做一些处理,使用getClass().getResourceAsStream()一直拿不到文件. 具体的用法. 1 InputStream is = this.getClass( ...

  8. Eclipse 安装JavaEE插件

    Oxygen版Eclipse 导入项目会自动安装你项目需要的一些插件,但是有时候会安装失败,需要手动安装: 这里以Dynamic Web Project项目为例 打开Eclipse,依次选择Help ...

  9. python 的None 探究

    a = None b = None print id(a),id(b),id(None) # 9430224 9430224 9430224 可能在别的环境下运行不是这个数,但是这三个数应该是一样的. ...

  10. Solving SharePoint Server 2010 - 503. The service is unavailable, After installation

    Installed: SharePoint Server 2010 for Internet Enterprise Beta (x64) On: Windows Server 2008 Standar ...