[Euler Loop] poj 1300 door man

Source: Internet
Author: User
Tags cmath

Question link:

Http://poj.org/problem? Id = 1300

Door Man
Time limit:1000 ms   Memory limit:10000 K
Total submissions:2137   Accepted:857

Description

You are a butler in a large mansion. this mansion has so far than rooms that they are merely referred to by number (room 0, 1, 2, 3, Etc ...). your master is a participant ly absent-minded lout and continually leaves doors open throughout a participant 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.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 3 components:

  1. Start line-a single line, "Start m n", where M indicates the butler's starting room, and n indicates the number of rooms in the house (1 <= n <= 20 ).
  2. Room list-a series of n lines. each line lists, for a single room, every open door that leads to a room of higher number. for example, if Room 3 had open doors to Room 1, 5, and 7, the line for Room 3 wocould read "5 7 ". the first line in the list represents room 0. the second line represents Room 1, and so on until the last line, which represents room (n-1 ). it is possible for lines to be empty (In particle, the last line will always be empty since it is the highest numbered room ). on each line, the adjacent rooms are always listed in ascending order. it is possible for rooms to be connected by Multiple doors!
  3. End Line-a single line, "end"

Following the final data set will be a single line, "endofinput ".

Note that there will be no more than 100 doors in any single data set.

Output

For each data set, there will be exactly one line of output. if it is possible for the Butler (by following the rules in the Introduction) to walk into his chambers and close the final open door behind him, print a line "Yes X ", where X is the number of doors he closed. otherwise, print "no ".

Sample Input

START 1 21ENDSTART 0 51 2 2 3 3 4 4ENDSTART 0 101 923456789ENDENDOFINPUT

Sample output

YES 1NOYES 10

Source

South Central USA 2002

[Submit] [Go Back] [Status] [discuss]

Question meaning:

There are n rooms, and the rooms are connected by the door to know the room connected by the door. Check whether room m can pass all the doors once and return to door 0.

Solution:

Think of the room as a node and the door as an edge. The question is a connected graph, and then judge whether there is an Euler's path from m to 0.

Calculate the degree of each point.

Code:

//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 22int nu[Maxn],n,m;char temp[Maxn];int main(){    //freopen("in.txt","r",stdin);   //freopen("out.txt","w",stdout);   while(scanf("%s",temp))   {       if(temp[3]=='O')            break;       scanf("%d%d",&m,&n);       memset(nu,0,sizeof(nu));       getchar();       int ans=0;       for(int i=0;i<n;i++)       {           int la=0;           char c;           while((c=getchar())!='\n')           {               if(c==' ')               {                   ans++;                   nu[i]++;                   nu[la]++;                   la=0;                   while((c=getchar())==' ');                   if(c=='\n')                        break;                   la=c-'0';               }               else                  la=la*10+c-'0';           }           if(la)           {               nu[i]++;               nu[la]++;               ans++;           }       }       //printf("ans:%d :%d %d\n",ans,nu[0],nu[1]);       //system("pause");       int ocnt=0,a[3];       for(int i=0;i<n;i++)           if(nu[i]&1)           {               ocnt++;               if(ocnt>2)                    break;               a[ocnt]=i;           }        gets(temp);        if(ocnt>2||ocnt==1)            printf("NO\n");        else if(!ocnt)        {            if(!m)                printf("YES %d\n",ans);            else                printf("NO\n");        }        else        {            if(a[1]>a[2])                swap(a[1],a[2]);            if(!a[1]&&a[2]==m)                printf("YES %d\n",ans);            else                printf("NO\n");        }   }    return 0;}



[Euler Loop] poj 1300 door man

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.