POJ 1300 最基础的欧拉回路问题

题目大意:

从0~n-1编号的房间,从一个起点开始最后到达0号房间,每经过一扇门就关上,问最后能否通过所有门且到达0号房间

我觉得这道题的输入输出格式是我第一次遇到,所以在sscanf上也看了很久

每一行对应当前门能到达的房间,下方如有重复不在输入,所以会有空行,这里的空行,和将字符串内的数字一个个代入需要好好斟酌

这里只有两种情况能成功

从 0号房间出发,经过一个欧拉回路到达0

从别的房间出发,一个欧拉通路到达0,2个端点的均为基度节点

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int door[22];
 6 int readLine(char *s){
 7     int L;
 8     for(L=0;(s[L]=getchar())!=‘\n‘&&s[L]!=EOF;L++);
 9     s[L]=0;
10     return L;
11 }
12 int main()
13 {
14     char buf[128];
15     int m,n;
16     while(readLine(buf)){
17         if(buf[0]==‘S‘){
18             sscanf(buf,"%*s %d %d",&m,&n);
19             memset(door,0,sizeof(door));
20
21             int numOfDoor=0;//记录所有门的数量,为了最后结果输出总共关上的门的数目
22             for(int i=0;i<n;i++){
23                 readLine(buf);
24                 int k=0,j;//读取数据在字符串中的指针位置
25                 while(sscanf(buf+k,"%d",&j)==1){
26                     door[i]++,door[j]++;
27                     numOfDoor++;
28                     while(buf[k]&&buf[k]==‘ ‘)k++;
29                     while(buf[k]&&buf[k]!=‘ ‘)k++;
30                 }
31             }
32             readLine(buf);//读入END
33             int odd=0,even=0;
34             for(int i=0;i<n;i++){
35                 if(door[i]%2!=0)odd++;
36                 else even++;
37             }
38             if(odd==0&&m==0) printf("YES %d\n",numOfDoor);
39             else if(odd==2&&door[m]%2==1&&door[0]%2==1&&m!=0) printf("YES %d\n",numOfDoor);
40             else puts("NO");
41         }
42         else if(!strcmp(buf,"ENDOFINPUT")) break;
43     }
44     return 0;
45 }
时间: 2024-08-28 00:16:07

POJ 1300 最基础的欧拉回路问题的相关文章

POJ 1300 Door Man(欧拉回路的判定)

题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉通路的问题了,但实际上,就只有两种情况能够输出YES,以房间为顶点,连接房间之间的门为边构造图,这两种情况分别是存在欧拉回路和欧拉通路的情况:所有房间都是偶数个门并且起始房间就是0,所以可以回到0,存在欧拉回路:有两个房间的门是奇数个,其余都是偶数个,这种情况下,要求出发房间和0房间的门是奇数个,并

POJ 1300 Door Man(欧拉回路_格式控制*)

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 part

POJ 1300 Door Man

判断是否欧拉回路. 很蛋疼的一道题,加上DFS判所有点是否连通就无限WA.(并查集也可判) 直接定理就AC了.都不知道所有点是不是在一个 连通块里面. 然后他们说:Your master is a particularly absent-minded lout and continually leaves doors open throughout a particular floor of the house. 这句话就表明了连通--问题是,中间关几个门让他无法通过,前后都有门没关怎么办--

poj 1041 John&#39;s trip 欧拉回路

题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include

POJ - 1392 Ouroboros Snake (欧拉回路的应用)

Description Ouroboros is a mythical snake from ancient Egypt. It has its tail in its mouth and continously devours itself. The Ouroboros numbers are binary numbers of 2^n bits that have the property of "generating" the whole set of numbers from

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 转:kuangbing 无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节点的度为偶数,或者有且只有两个度为奇数的节点. 图的连通可以利用并查集去判断. 度数的统计比较容易. view code//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针 //做这道题,知道了欧拉回路判定,还有用指针写trie #include

POJ 2608 Soundex 基础题题解

基本的编程能力考查. 注意: 1 下标处理 2 审查题意,并严格根据题意去重. 3 如何把代码写清晰精简. #include <stdio.h> #include <string.h> const short MAX_LETTER = 21; const short ALP_LEN = 26; short Letter[ALP_LEN] = {-1, 1, 2, 3, -1, 1, 2, -1, -1, 2, 2, 4, 5, 5, -1, 1, 2, 6, 2, 3, -1, 1

poj 1300 Door Man 欧拉回路

题目链接:http://poj.org/problem?id=1300 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 door

[欧拉回路] poj 1300 Door Man

题目链接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2137   Accepted: 857 Description You are a butler in a large mansion. This mansion has so many rooms that they are merely referred to by number