POJ--1300 -- Door Man [Determining Euler's path], Euler's path

Source: Internet
Author: User

POJ--1300 -- Door Man [Determining Euler's path], Euler's path

Link: http://poj.org/problem? Id = 1300

There are n rooms. Each room has several doors connected to other rooms. The butler starts from room m and wants to return to his residence (0 ), ask if there is a way to walk all the doors without repeating them.


Idea: Determine whether there is an Euler path, based on the nature of Euler's path and Euler's loop. There are two situations: one is the Euler loop, where the number of doors in all rooms is an even number, and the initial room is not 0 at this time, there is a required path, if the initial value is 0, no. The other is the Euler's path. There are only an odd number of doors in the two rooms, and the remaining two are even numbers. One of the two rooms is 0, the other is the current starting point, and the start point cannot be 0, the required path also exists. Otherwise, the path does not exist.

It hurts

#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 500100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int in[30],out[30];char s[20],s1[200];int main(){    int i,j;    int m,n;    int a,flag,ans,fk;    while(scanf("%s",s)!=EOF){        if(s[0]=='E'&&strlen(s)>5)  break;        scanf("%d%d",&m,&n);        getchar();        ans = 0;        flag = 0;        fk = 0;        memset(in,0,sizeof(in));        for(i=0;i<n+1;i++){            gets(s1);            int p = 0;            while(sscanf(s1+p,"%d",&a)==1){                ans++;                in[a]++;                in[i]++;                while(s1[p]!='\0'&&s1[p]!=' ')  p++;                while(s1[p]!='\0'&&s1[p]==' ')  p++;            }        }        for(i=0;i<n;i++){            if(in[i]&1) flag++;        }        if(!flag){            if(!m)  fk = 1;            else    fk = 0;        }        else{            if(flag==2&&m!=0&&in[m]&1&&in[0]&1)   fk = 1;            else    fk = 0;        }        if(fk)  printf("YES %d\n",ans);        else    puts("NO");    }    return 0;}



What is the difference between the Euler's path and the Hamilton path?

Euler's path: if there is a path that passes through each side of the graph once and only once, this circuit is called Euler's path.

Hamilton path: given the distance (or weight) between n points and n points, a loop is obtained so that it passes through all points and passes through each point only once, the total distance (or total weight) of the entire loop (also known as path or border) is the smallest.

(Discrete Mathematics) What is the difference between the Euler's path (loop) and the Hamilton path (loop?

From their definitions, we can see the difference: Euler's path refers to the process of passing through each edge ......, While the Hamilton path is through every vertex ......

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.