POJ1300 Door Man Euler Loop judgment, poj1300door
Question link:
1300
Question:
In a house (No. 0 ~ N-1) N rooms and X doors connecting two rooms, all rooms are connected, and the door will be closed every time a door passes. Q: Can a person depart from room M successfully reach room 0 and close all doors.
Question:
This is an entry to the Euler loop. First, we will learn the judgment theorem of the undirected Euler Loop:
An undirected graph G has an Euler's path. The condition is that G is a connected graph, and G has only two odd-degree nodes (vertices with odd numbers) or no odd-degree nodes.
This is an undirected graph with a fixed start point and end point.
Therefore, there are only two situations that can constitute the Euler Loop:
1 The degrees of all vertices are even
2. the start point and end points are odd and the other values are even.
Code:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){ int degree[30]; int m,n,i,j,s,odd,even; char str[1005],str2[1005]; while(scanf("%s",str)&&strcmp(str,"ENDOFINPUT")!=0) { s=odd=even=0; memset(degree,0,sizeof(degree)); scanf("%d%d",&m,&n); getchar(); for(i=0; i<n; i++) { gets(str2); for(j=0; j<strlen(str2); j++) if(str2[j]>='0'&&str2[j]<='9') { s++; degree[i]++; degree[str2[j]-'0']++; } } scanf("%s",str); for(i=0; i<n; i++) { if(degree[i]%2==0) even++; else odd++; } if((odd==0&&m==0)||(odd==2&°ree[m]%2==1&°ree[0]%2==1&&m!=0)) cout<<"YES"<<' '<<s<<endl; else cout<<"NO"<<endl; } return 0;}