poj 1300 Door Man 欧拉回路

时间:2023-03-09 00:04:56
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 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.

题意:给出一些房间和房间上的门以及房间之间的路径,如果你沿着这条路走到另一个房间,那么就得关掉这条路上的门,关掉的门不能再打开,问能不能从0号房间,在关掉所有门之后刚好走到房间m。

解法:欧拉回路的判断。

无向图的欧拉回路:连通无向图中没有度数为奇数个的节点或者有且仅有两个奇数个节点(此时这两个节点必须一个为起点,另一个为终点)。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=; int m,n,degree[maxn];
char str[maxn],s[maxn]; int main()
{
while (scanf("%s",str)!=EOF)
{
if (strcmp(str,"ENDOFINPUT")==) break;
scanf("%d%d",&m,&n);
int a;
memset(degree,,sizeof(degree));
memset(s,,sizeof(s));
int cnt=;
getchar();
for (int i= ;i<n ;i++)
{
gets(s);
int len=strlen(s);
if (len==) continue;
a=;
int q=;
for (q= ;q<len ;q++) if (s[q]!=' ') break;
for (int j=q ;j<len ;j++)
{
if (s[j]==' ')
{
degree[i] ++ ;
degree[a] ++ ;
cnt ++ ;
a=;continue;
}
a=a*+s[j]-'';
if (j==len-)
{
degree[i] ++ ;
degree[a] ++ ;
cnt ++ ;
}
}
}
scanf("%s",str);
a=;
int k=-,k2=-;
for (int i= ;i<n ;i++)
{
if (degree[i]%)
{
a++;
if (k==-) k=i;
else k2=i;
}
}
if (a> || a==) printf("NO\n");
else if (a==)
{
if ((k== && k2==m)||(k==m && k2==)) printf("YES %d\n",cnt);
else printf("NO\n");
}
else if (m==) printf("YES %d\n",cnt);
else printf("NO\n");
}
return ;
}