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. 在visual code的debugger for chrome中调试webpack构建的项目

    一直使用chrome中内置的调试器, 感觉世界那么美好, 自从学了react之后,使用visual code作为编辑器, 它提供了很多插件, 其中就包括debugger for chrome, 一款使 ...

  2. html文字在django模板中取消转译

    django {{ news.content | safe }} 还有escape=fales,具体咋用,有点蒙蔽

  3. FlexPaper及二次开发

    Flexpaper二次开发入门教程 http://ajava.org/course/web/?page=2

  4. linux更新系统

    1.linux更新系统 https://www.jb51.net/os/RedHat/450223.html 仅更新系统,不更新内核 yum -y --exclude=kernel\* upgrade ...

  5. leetcode210

    public class Solution { //test case [1,0] public int[] findOrder(int numCourses, int[][] prerequisit ...

  6. bootstrap做的导航

    顶部导航:nav-tabs 左边导航:nav-list 响应式布局:div嵌套 ~ container.row.ol-lg-X 效果: 源码: <!DOCTYPE html> <ht ...

  7. is not on any development teams

    is not on any development teams 1)账号正在申请中 2)申请成功后的账号? 加了3个账号,都是这样子的. 1:Xcode>Window> "Org ...

  8. xe DateTimePicker.Date bug

    xe6 bug xe7 ok DateTimePicker1->DateTime.DateString(); DateTimePicker1->DateTime.DateTimeStrin ...

  9. 生产者和消费者模式--java设计模式

    生产者和消费者: 就犹如在快餐店点餐一样,有多个打饭的,有不定时的人来买饭,买饭的人从快餐店自动取餐,如果快餐的库存数量达到下限值时,自动启动打饭的,补充盒饭. 通过while循环的方式,传入变量is ...

  10. eclipse xDoclet错误

    转载: http://blog.csdn.net/lovelongjun/article/details/53485773 Missing library: xdoclet-1.2.1.jar. Se ...