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. django 更新 模板语言

    Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 在Django的模板语言中按此语法使用:{{ 变量名 ...

  2. 在CentOS7.4中安装jdk的几种方法及配置环境变量

    在CentOS7.4中安装jdk的几种方法及配置环境变量 一.下载jdk jdk下载地址:http://www.oracle.com/technetwork/java/javase/downloads ...

  3. Dep数据发布,推送

    package com.cfets.ts.u.shchgateway.util; import com.cfets.cwap.s.stp.MessageUnit; import com.cfets.t ...

  4. 怎么爆加密过后的前端JS

    网站地址,保密,下面进入主题 页面元素和JS代码 无法看 用谷歌浏览器,选择器选中提交按钮,注意看界面元素源码 onclick="$('#submitForm').submit()" ...

  5. 5.log4j报错

    java.lang.UnsupportedClassVersionError: org/apache/log4j/Logger : Unsupported major.minor version 51 ...

  6. eclipse 的project explorer问题,这个怎样把localFileSystem去掉

    转自:https://zhidao.baidu.com/question/550279043.html

  7. (Python)numpy的argmax用法

      解释 还是从一维数组出发.看下面的例子. import numpy as np a = np.array([3, 1, 2, 4, 6, 1]) print(np.argmax(a))4 argm ...

  8. XSS学习小结

    一.什么是XSS? XSS全称是Cross Site Scripting即跨站脚本,当目标网站目标用户浏览器渲染HTML文档的过程中,出现了不被预期的脚本指令并执行时,XSS就发生了. 这里我们主要注 ...

  9. 2018.8.14-C#复习笔记总

    using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...

  10. 管道限流利器pv

    pv 是什么 可不是 page view,是pipe viewer,管道偷窥器的缩写.这个东西的源站点在google code上,需要的话可以访问pv 的官网 . 这个东西的官方手册页(man pv或 ...