POJ1300 Door Man Euler Loop judgment, poj1300door

Source: Internet
Author: User

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&&degree[m]%2==1&&degree[0]%2==1&&m!=0))            cout<<"YES"<<' '<<s<<endl;        else            cout<<"NO"<<endl;    }    return 0;}




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.