//2009-05-14 10:26:41 Accepted 1395 C++ 0 184
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;

const int N = 25;

int main()


{
char ch[100];
int m, n, cnt= 0;
int i, j, len, a, odd_cnt;
int degree[N];

scanf("%s", ch);

while(strcmp(ch, "ENDOFINPUT") != 0)

{
//M indicates the butler's starting room, and N indicates
//the number of rooms in the house (1 <= N <= 20).
scanf("%d%d",&m, &n);
cnt = 0;
memset(degree, 0, sizeof(degree));
getchar();
for(i = 0; i < n; i++)

{
gets(ch);//getchar();
len = strlen(ch);
for(j = 0;j < len; j++)

{
if(isdigit(ch[j]))

{
a = atoi(ch + j);
while(isdigit(ch[j]) && j < len)
j++;
degree[i]++;
degree[a]++;
cnt++;
}
}
}
gets(ch);

for(odd_cnt = 0, i = 0; i < n; i++)

{
if(odd_cnt > 2)
break;
if(degree[i] % 2 != 0)
odd_cnt++;
}

if(odd_cnt > 2 || odd_cnt == 1)

{//不能形成欧拉通路,也不能形成欧拉回路的情况
printf("NO\n");
}
else if(odd_cnt == 2)

{//对形成欧拉通路的情况分析
if(m != 0 && degree[m] %2 != 0 && degree[0] % 2 != 0)
printf("YES %d\n", cnt);
else
printf("NO\n");
}
else

{//对行程欧拉回路的情况分析
if(m == 0)
printf("YES %d\n", cnt);
else
printf("NO\n");

}

scanf("%s", ch);

}
return 0;
}
转载于:https://www.cnblogs.com/Xredman/archive/2009/05/14/1456574.html