Poj 1300 door man (Euler's loop determination)

Source: Internet
Author: User

Question Link

There are many rooms in the manor, ranging from 0 to n-1. Can you find a path to go through all open doors and close the door after passing through the door, return to the room numbered 0.

Idea: This is a naked issue of determining the Euler's path, but in fact, there are only two situations where yes can be output, with the room as the vertex and the door connecting the room as the edge structure, the two situations are Euler's circuit and Euler's path respectively: all rooms have even numbers of doors and the starting room is 0, so we can return to 0 and there is Euler's circuit; there are two rooms with an odd number of doors and the others with an even number. In this case, the number of doors required for the departure room and the number of rooms 0 is an odd number, and the number of rooms cannot be 0, because there is no Euler loop from 0 to 0, but there is an Euler path from another room to 0.

 1 //POJ 1300 2 #include <stdio.h> 3 #include <string> 4 #include <iostream> 5 #include <string.h> 6  7 using namespace std ; 8  9 int M,N,door[20] ;10 string sh ;11 char sh1[789] ;12 int main()13 {14     while(cin >> sh)15     {16         if(sh == "ENDOFINPUT")17             break ;18         cin >> M >> N ;19         getchar() ;20         int cnt = 0 ;21         memset(door,0,sizeof(door)) ;22         for(int i = 0 ; i < N ; i++)23         {24             gets(sh1) ;25             int len = strlen(sh1) ;26             for(int j = 0 ; j < len ; j++)27             {28                 if(sh1[j] != ‘ ‘)29                 {30                     int d = sh1[j]-‘0‘ ;31                     cnt ++ ;32                     door[i] ++ ;33                     door[d] ++ ;34                 }35             }36         }37         cin >> sh ;38         int odd = 0 ,even = 0 ;39         for(int i = 0 ; i < N ; i++)40         {41             if(door[i] % 2) odd ++ ;42             else even ++ ;43         }44         if(odd == 0 && M == 0)45             cout<< "YES "<< cnt <<endl ;46         else if(odd == 2 && M != 0)47             cout << "YES "<<cnt <<endl ;48         else cout<<"NO"<<endl ;49     }50     return 0 ;51 }
View code

 

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.