Door Man
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2639   Accepted: 1071

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
【分析】这一题就是个欧拉回路的判定,很简单,但是输入有点麻烦。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
ll power(ll a,int b,ll c){ll ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
char str[N];
int n,m,cnt[];
int main()
{
while(gets(str)!=NULL){
if(!strcmp(str,"ENDOFINPUT"))break;
sscanf(str,"%*s%d%d",&m,&n);
memset(cnt,,sizeof(cnt));
int ans=;
for(int i=;i<n;i++){
gets(str);
int k=,j;
while(sscanf(str+k,"%d",&j)==){
ans++;
cnt[i]++;
cnt[j]++;
while(str[k]&&str[k]==' ')k++;
while(str[k]&&str[k]!=' ')k++;
}
}
gets(str);
int odd=,even=;
for(int i=;i<n;i++){
if(cnt[i]&)odd++;
else even++;
}
if(!odd&&!m)printf("YES %d\n",ans);
else if(odd==&&(cnt[m]&)&&(cnt[]&)&&m)printf("YES %d\n",ans);
else printf("NO\n");
} return ;
}

POJ1300Door Man(欧拉回路)的更多相关文章

  1. ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)

    两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...

  2. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  3. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  4. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  5. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  6. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  7. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  8. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 10054 (欧拉回路) The Necklace

    题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...

随机推荐

  1. 【TMD模拟赛】上低音号 链表

    这道题一看有两个出发现点,一枚举点去找边界,想了一会就Pass了...,二是枚举相框,我们最起码枚举两个边界,然后发现平行边界更好处理,然而仍然只有30分,这个时候就来到了链表的神奇应用,我们枚举上界 ...

  2. git使用笔记(八)团队协作

    By francis_hao    Nov 24,2016       本文由 刘英皓 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可.欢迎转载,请注明出处 ...

  3. HDU1054 Strategic Game(最小点覆盖)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. import pymongo exceptions.ImportError: No module named pymongo

    最近用Scrapy写爬虫,将爬取的数据存入Mongodb中,使用的是pymongo这个库,但是运行的时候报错如标题所示 搜了好多网站包括stackoverflow都没有解决,后来发现自己用的是虚拟环境 ...

  5. APP本地服务安全测试

    一.安全测试基本分类: 1.系统安全 系统加固 安全加固:比如linux中关闭telnet端口,修改ssh端口 检测一些不必要的服务(需要卸载一个ping)--保证系统的最小集 app安全加固:加一层 ...

  6. Linux shell命令无法使用

    配置java环境变量时,把PATH=$PATH:$HOME/bin中的冒号“:”错写成分号“;”.导致变量PATH配置错误,不能正确找到shell命令的文件位置.把/etc/profile 中java ...

  7. Http/2 升级指南

    [转]http://www.syyong.com/architecture/http2.html HTTP/2(最初名为HTTP/2.0)是 WWW 使用的 HTTP 网络协议的主要版本. 它来自早先 ...

  8. 好几次的CSS存档

    第一次: #site_nav_under { display: none; } .c_ad_block, .ad_text_commentbox { display: none; margin:; p ...

  9. bzoj 1208 HNOI2004宠物收养所 平衡树

    裸平衡树,恢复手感用的 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, left, ...

  10. Linux下的两个经典宏定义【转】

    转自:http://www.linuxidc.com/Linux/2015-07/120014.htm 本文首先介绍Linux下的经典宏定义,感受极客的智慧,然后根据该经典定义为下篇文章作铺垫. of ...