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. number 解题报告

    number 题目描述 给定整数 \(m,k\),求出正整数 \(n\) 使得 \(n+1,n+2,-,2n\) 中恰好有 \(m\) 个数在二进制下恰好有 \(k\) 个 \(1\). 有多组数据. ...

  2. I/O多路转接-epoll

    By francis_hao    Aug 5,2017   APUE讲多路转接的章节介绍了select.pselect和poll函数.而epoll是linux内核在2.5.44引入的.在glibc ...

  3. POJ1062:昂贵的聘礼(dfs)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 58108   Accepted: 17536 题目链接:http ...

  4. 理解JWT(JSON Web Token)认证及python实践

    原文:https://segmentfault.com/a/1190000010312468?utm_source=tag-newest 几种常用的认证机制 HTTP Basic Auth HTTP ...

  5. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  6. DOM读取和修改节点对象属性

    一.获取和修改元素间的内容(3种) 1.innerHTML 获得/设置元素开始标签和结束标签之间的html原文 固定套路:1.删除父元素下所有子元素:parent.innerHTML="&q ...

  7. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  8. hashlib模块加密用法

    hashlib 加密模块 hashlib.md5() 构建一个md5的对象,用于调用对象的update方法去加密   例子: import hashlib hash = hashlib.md5() h ...

  9. ZOJ 3598 Spherical Triangle球面几何公式应用

    #include <map> #include <set> #include <list> #include <cmath> #include < ...

  10. Vmware中安装和卸载Linux 16.04.3

    1.先去这个链接 https://www.ubuntu.com/download ,下载ubuntu镜像,也就是下图的download选项 2.下载后到虚拟机里创建一个新的虚拟机,然后按照博客 htt ...