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的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...
随机推荐
- 【题解】NOI2009管道取珠
又是艰难想题的一晚,又是做不出来的一题 (:д:) 好想哭啊…… 这题最关键的一点还是提供一种全新的想法.看到平方和这种东西,真的不好dp.然而我一直陷在化式子的泥潭中出不来.平方能够联想到什么?原本 ...
- 洛谷 P2893 [USACO08FEB]修路Making the Grade 解题报告
P2893 [USACO08FEB]修路Making the Grade 题目描述 A straight dirt road connects two fields on FJ's farm, but ...
- AOJ.559 丢失的数字
丢失的数字 Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 1552 Submission Accepted: 273 Descri ...
- power designer 绘制E-R 图
总体概括:本篇主要先介绍E-R图的一些基本概念,然后介绍怎么绘制E-R图,特别是用power designer 的反向工程怎么把表中对字段的注释也展示出来. 1.E-R图的基本概念: E-R图就是en ...
- Ubuntu修改grub启动顺序和启动时间
sudo gedit /boot/grub/grub.cfg,输入密码,在弹出的文件中找到set default = "0",想要改为第N项默认就把0改成N-1 看到启动界面是第几 ...
- c++(类)构造函数、复制构造函数
复制构造函数是一种特殊的构造函数,它的作用是用一个已经存在的对象去初始化另一个对象.一般情况下不需要自行定义复制构造函数,系统默认提供一个逐个复制成员值的复制构造函数. 何时要使用呢? 1.将新对象初 ...
- es6+最佳入门实践(12)
12.class基础用法和继承 12.1.class基础语法 在es5中,面向对象我们通常写成这样 function Person(name,age) { this.name = name; this ...
- Android百度定位API的使用
导入库文件 在下载页面下载最新的库文件.将liblocSDK2.4.so文件拷贝到libs/armeabi目录下.将locSDK2.4.jar文件拷贝到工程根目录下,并在工程属性->Java B ...
- PostgreSQL(Linux)安装、启动、停止、重启
If we don't already have PostgreSQL installed, we must install it. $ sudo apt-get install postgresql ...
- 知问前端——日历UI(一)
日历(datepicker)UI,可以让用户更加直观的.更加方便的输入日期,并且还考虑不同国家的语言限制,包括汉语. 调用datepicker()方法 $('#date').datepicker(); ...