欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2476 Accepted: 1001
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:
Always shut open doors behind you immediately after passing through
Never open a closed door
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:
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).
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!
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
Source
South Central USA 2002
题意:给你起点m和房间的个数n,接下来有n行,第一行代表房间0与之相连的房间编号(只列出比它大的房间编号,按升序),一个房间可能有多个门,问可从起点出发将所有的门关上,并回到房间0,如果可以关上输出”YES X”,X代表关上的门的数量,不能输出”NO”.
方法:一道无向图的欧拉回路的题,判断是不是欧拉回路,就看图的度,如果度为奇数的个数为零,则无论从哪一个点出发都可以回到0,如果度为奇数的为2个,则这两个点一个为起点一个为终点,需要判断起点是不是度为奇数并且0的度也为零,(m!=0).其余的情况都不可以.这个题的数据处理比较麻烦点.
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#pragma comment(linker, "/STACK:102400000")
#define WW freopen("output.txt","w",stdout)
const int MAX = 11000;
char buf[MAX];
char s[50];
int Du[120];
int ReadLine()
{
int L;
for(L=0;(buf[L]=getchar())!='\n'&&buf[L]!=EOF;L++);
buf[L]='\0';
return L;
}
int main()
{
int n,m;
int door;
while(ReadLine())
{
if(buf[0]=='S')
{
sscanf(buf,"%s %d %d",s,&m,&n);
memset(Du,0,sizeof(Du));
door=0;
for(int i=0;i<n;i++)
{
ReadLine();
int k=0;
int v;
while(sscanf(buf+k,"%d",&v)==1)
{
door++;
Du[i]++;
Du[v]++;
while(buf[k]&&buf[k++]==' ');
while(buf[k]&&buf[k++]!=' ');
}
}
ReadLine();
int ood=0;
for(int i=0;i<n;i++)
{
if(Du[i]&1)
{
ood++;
}
}
if((ood==0&&m==0)||(ood==2&&Du[m]&1&&Du[0]&1&&m))
{
printf("YES %d\n",door);
}
else
{
printf("NO\n");
}
}
else if(!strcmp(buf,"ENDOFINPUT"))
{
break;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏的更多相关文章
- 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...
- 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- 全面解析sizeof(下) 分类: C/C++ StudyNotes 2015-06-15 10:45 263人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof作用于基本数据类型,在特定的平台和特定的编译中,结果是确定的,如果使用sizeof计算构造类型:结构体.联合体和类的大小时 ...
- 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...
- MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏
业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...
- 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏
C语言中结构体作为函数参数,有两种方式:传值和传址. 1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下 ...
- Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏
海量请求,高性能服务器. 对比Apache, Apache:稳定,开源,跨平台,重量级,不支持高度并发的web服务器. 由此,出现了Lighttpd与Nignx:轻量级,高性能. 发音:engine ...
- jQuery中的on()和click()的区别 分类: 前端 HTML jQuery 2014-11-06 10:26 96人阅读 评论(0) 收藏
HTML页面代码 <div> <h1>Click</h1> <button class="add">Click me to add ...
随机推荐
- Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...
- IDEA SDK(Software Development Kit) 介绍
如上图标注 1 所示,IntelliJ IDEA 支持 6 种 SDK.最常用的就是 JDK 和 Android SDK,其中在创建 Android SDK 的时候如果你没有先配置一个 JDK 的话, ...
- G面经prepare: BuyGoods
给你一部分钱和一些不同价钱的商品,如何在最多买K件商品的情况下尽可能多的花掉手里的钱. 举例:口袋里的钱数: 10; K=2 产品价格: [3, 6, 8, 7, 9] 输出 3, 7 Backtra ...
- JAVA-面向对象-特性
1.封装 1.定义方式 1修饰符class类名 2类名首字母大写 2.类的成员 1属性 成员变量 可以设置默认值 第一个单词首字母小写,后面首字母大写 一般把属性设置成private 提供属性对应的g ...
- 数据库SQL 查询
查询 1.简单查询 select * from info(表名) --查所有数据 select code(列名),name(列名) from 表名 --查指定列的数据 selec ...
- BZOJ1930 [Shoi2003]pacman 吃豆豆
dp,首先建出图,f[i][j]表示a吃到了i点,b吃到了j点的最大值,转移的时候转移拓扑序小的那一维,如果i拓扑序小于j,那么转移到f[k][j],否则转移到f[i][k],建出的图边数也要优化, ...
- Android设计模式---观察者模式小demo(一)
1,今天刚好看到了设计模式这一块来,而观察者模式是我一直想总结的,先来看看观察者模式的简单的定义吧 "当一个对象改变时,他的所有依赖者都会受到通知,并自动更新." 一般我们项目中就 ...
- JAVA通过poi对Excel数据在(jsp+ssh)环境下导入导出
POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bin-2.5.1-final-20040 ...
- paper 88:人脸检测和识别的Web服务API
本文汇总了全球范围内提供基于Web服务的人脸检测和识别的API,便于网络中快速部署和人脸相关的一些应用. 1:从How-old的火爆说起 最开始,网站的开发者只是给一个几百人的群发送email,请他们 ...
- shell 命令