Door Mans
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 2596 |
|
Accepted: 1046 |
Description
You is a butler in a large mansion. This mansion have so many rooms that they is merely referred to by number (0, 1, 2, 3, etc ...). Your Master is a particularly absent-minded lout and continually leaves doors open throughout a particular floor of the Ho Use. Over the years, you had mastered the art of traveling in a single path through the sloppy rooms and closing the doors beh IND you. Your biggest problem is determining whether it's possible to find a path through the sloppy rooms where you:
- Always shut open doors behind-immediately after passing through
- Never open a closed door
- End up in your chambers (guest 0) with all doors closed
In this problem, you is given a list of rooms and open doors between them (along with a starting). It is not needed to determine a route, only if one is possible.
Input
Input to this problem would consist of a (Non-empty) series of up to data sets. Each data set is formatted according to the following description, and there would be no blank lines separating data s Ets.
A Single Data set have 3 components:
- Start Line-a single line, "Start M N", where m indicates the Butler ' s starting, and N indicates the number of rooms In the House (1 <= N <= 20).
- The List-a series of N lines. Each line lists, the for a single, every open door the leads to a, the higher number. For example, if the box 3 had open doors to rooms 1, 5, and 7, the line for box 3 would read "5 7". The first line in the list represents 0. The second line represents guest 1, and so on until the last line, which represents (N-1). It's possible for lines to being empty (in particular, the last line would always be empty since it's the highest numbered R OOM). On all line, the adjacent rooms is always listed in ascending order. It's possible for rooms to being connected by multiple doors!
- End Line-a single line, "End"
Following the final data set would be a, "endofinput".
Note that there'll be is no more than the doors in any single data set.
Output
For each data set, there'll is exactly one line of output. If It is a possible for the butler (by following the "rules in the introduction") to walk into his chambers and close the FINA L Open door behind him, print a line "YES X", where X is the number of the doors he closed. Otherwise, print "NO".
Sample Input
START 1 21ENDSTART 0 2 2 3 3 4 4ENDSTART 0 101 923456789ENDENDOFINPUT
Sample Output
YES 1NOYES 10
Title Link: http://poj.org/problem?id=1300
Test instructions: There are many rooms, numbered 0,1,2,3,...。 Enter M,n. M is the starting room number; n is the total number of rooms (1≤n≤20). The next n ranks out of the room leading to the other room number. (Note: The empty line is also input, indicating that the room to the other room number has already appeared.) ) starting from M through all the doors, through the door closed, closed the door cannot open, and finally back to 0.
Idea: the most basic Euler path judgment, judging whether it is the starting point is M, the end point is 0 Oraton road (the default is connected graph). It is important to note the format of the input and output.
Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intdoor[ -];Chars[100000];intInit () {intlen=0; Charch; while((Ch=getchar ()) &&ch!='\ n') {S[len++]=ch; } returnLen;}intMain () {intI,j,m,n; intLen; intans=0; while(SCANF ("%s", s)! =EOF) {Len=strlen (s); if(len==Ten) Break; scanf ("%d%d",&m,&N); GetChar (); memset (Door,0,sizeof(door)); Ans=0; for(i=0; i<n; i++) {len=Init (); for(j=0; j<len; J + +) { intnum=0; while(j<len&&s[j]!=' ') {num=num*Ten+ (s[j]-'0'); J++; } ans++; Door[i]++; Door[num]++; }} scanf ("%s", s); inteven=0, odd=0; for(i=0; i<n; i++) { if(door[i]%2==0) even++; Elseodd++; } if(odd==0&&m==0) cout<<"YES"<<ans<<Endl; Else if(odd==2&&m!=0&&door[0]%2!=0&&door[m]%2!=0) cout<<"YES"<<ans<<Endl; Elsecout<<"NO"<<Endl; } return 0;}
View Code
POJ 1300.Door man Euler pathway