POJ1300Door Man(欧拉回路)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2639 | Accepted: 1071 |
Description
- 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
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
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(欧拉回路)的更多相关文章
- ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)
两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10054 (欧拉回路) The Necklace
题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...
随机推荐
- win7中输入文件夹首字母跳到相应的文件或者文件夹,却在搜索栏出现输入的字母
组织->文件夹和搜索选项->查看->在视图中选择键入项
- ionic2-键盘覆盖输入框和返回键问题解决方案
http://blog.csdn.net/u012979009/article/details/52514892有遇到这个问题的去这个地址看
- 使用es6一行搞定文字溢出省略号
使用的是es6中扩展字符串方法参考地址 padStart(),padEnd() 使用padStart() 两个参数padStart(字符串最小长度,用来补全的字符串): 例如 let str = '1 ...
- Angular 遍历循环数组
var app = angular.module('Mywind',['ui.router']) app.controller('Myautumn',function($scope,$http,$fi ...
- MySQL:BlackHole
MySQL:BlackHole 顾名思义BlackHole就是黑洞,只有写入没有输出.现在就来实验一下吧 首先查看一下MySQL支持的存储引擎 mysql> show engines;+---- ...
- Educational Codeforces Round 59 (Rated for Div. 2) DE题解
Educational Codeforces Round 59 (Rated for Div. 2) D. Compression 题目链接:https://codeforces.com/contes ...
- 怎么利用idea自带的工具,不需要 重启tomcat或则其他服务,js代码自动生效
idea中有一个工具:可以直接upload,能让你修改的界面直接可以看到,不需要重启服务. 依次点击的按钮如下: 点击进入的界面这个填的只是一个示例,在各位的电脑上肯定不行,大家依据实际情况填写.
- pip 使用国内源
常用国内的pip源如下:阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...
- GDSOI2015的某道题目
分析: 看到这个$3^i$就觉得很奇怪的样子...为什么一定要是$3^i$...而且不能重复使用... 不能重复使用就代表不会产生进位,那么一定是若干个$3^i$相加减的式子... 仔细观察,我们发现 ...
- compositionstart 、 compositionend 、 input都存在时的解决办法
$(function () { var cpLock = true; $('#textbox').off().on({ compositionstart: function () {//中文输入开始 ...