POJ 1300 Euler's Path & amp; Euler's loop

Source: Internet
Author: User
Tags acos

The system learns graph theory once! Start with this blog!

First, introduce some concepts.

Undirected graph:

G is a connected undirected graph, and the path that passes through each edge of G once and only once is Euler's path.

If the Euler path is a loop (the start point and the end point are the same), this loop is called an Euler loop.

Undirected graph G with Euler's loop is called Euclidean.

 


Directed Graph:

D is the Directed Graph Connected to the baseline graph. It is called every edge passing through D and the path is directed Euler's path only once.

If this path is a loop, it is called a directed Euler loop.

Directed Graph D with directed Euler's loop is called directed Euclidean.

 


Undirected graph to determine the Euler path: G is a connected graph with only two odd-degree nodes or no odd-degree nodes.

If there are two odd points, these two points must be the start point and end point of the Euler's path.

If there are no odd-degree nodes, the Euler's loop is fixed in this figure.

 


Directed Graph to determine Euler's path: the base graph of D is connected, and the outbound degree of all nodes is the same as the inbound degree, so the graph is stored in a directed Euler's loop.

If the difference between the outbound and inbound degrees of only two nodes is 1 and-1, there must be an Euler's path in this figure, and it must start from the point where the difference between inbound and outbound degrees is-1, and the point where the difference between inbound and outbound degrees is 1 is the end point.

 


/*************************************** * ******************* The above concepts ***************** **************************************** *********/

 


Next we will introduce this question.

The question is whether a node can start from a point and go through all the edges and return to node 0.

The idea is to judge whether there is an Euler loop if the starting point is 0.

If the start point is not 0, it is to determine whether there is an Euler path, and the start point of the Euler path is 0 and the input start point.

 

#include <iostream>   #include <cstdio>   #include <algorithm>   #include <string>   #include <cmath>   #include <cstring>   #include <queue>   #include <set>   #include <vector>   #include <stack>   #include <map>   #include <iomanip>   #define PI acos(-1.0)   #define Max 2505   #define inf 1<<28   #define LL(x) ( x << 1 )   #define RR(x) ( x << 1 | 1 )   #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )   #define ll long long   #define mem(a,b) memset(a,b,sizeof(a))   #define mp(a,b) make_pair(a,b)   #define PII pair<int,int>   using namespace std;      inline void RD(int &ret) {      char c;      do {          c = getchar();      } while(c < '0' || c > '9') ;      ret = c - '0';      while((c=getchar()) >= '0' && c <= '9')          ret = ret * 10 + ( c - '0' );  }  inline void OT(int a){      if(a >= 10)OT(a / 10) ;      putchar(a % 10 + '0') ;  }  #define N 1111   char in[111] ;  int degree[N] ;  int main() {      while(cin >> in){          if(in[0] == 'E')break ;          mem(degree , 0) ;          int n , m ;          cin >> n >> m ;          getchar() ;          int sum = 0 ;          for (int i = 0 ; i < m ;i ++ ){              int now ;              gets(in) ;              int l = strlen(in) ;              if(!l)continue ;              int num = 0 ;              for (int j = 0  ;j < l ;j ++ ){                  if(in[j] == ' '){                      degree[i] ++ ;                      degree[num] ++ ;                      sum ++ ;                      num = 0 ;                  }                  else {                      num = num * 10 + (in[j] - '0') ;                  }              }              if(num){                  degree[i] ++ ;                  degree[num] ++ ;                  sum ++ ;              }          }          cin >> in ;          int odd = 0 ;          int even = 0 ;          for (int i = 0 ; i < m ; i ++ ){              if(degree[i] & 1)odd ++ ;              else even ++ ;          }          if(!odd){              if(n == 0){                  printf("YES %d\n",sum) ;              }              else {                  puts("NO") ;              }          }          else {              if(odd == 2){                  if((degree[n] & 1) && (degree[0] & 1) && (n != 0)){                      printf("YES %d\n" ,sum) ;                  }                  else {                      puts("NO") ;                  }              }else {                  puts("NO") ;              }          }      }      return 0 ;  }  #include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2505#define inf 1<<28#define LL(x) ( x << 1 )#define RR(x) ( x << 1 | 1 )#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )#define ll long long#define mem(a,b) memset(a,b,sizeof(a))#define mp(a,b) make_pair(a,b)#define PII pair<int,int>using namespace std;inline void RD(int &ret) {    char c;    do {        c = getchar();    } while(c < '0' || c > '9') ;    ret = c - '0';    while((c=getchar()) >= '0' && c <= '9')        ret = ret * 10 + ( c - '0' );}inline void OT(int a){    if(a >= 10)OT(a / 10) ;    putchar(a % 10 + '0') ;}#define N 1111char in[111] ;int degree[N] ;int main() {    while(cin >> in){        if(in[0] == 'E')break ;        mem(degree , 0) ;        int n , m ;        cin >> n >> m ;        getchar() ;        int sum = 0 ;        for (int i = 0 ; i < m ;i ++ ){            int now ;            gets(in) ;            int l = strlen(in) ;            if(!l)continue ;            int num = 0 ;            for (int j = 0  ;j < l ;j ++ ){                if(in[j] == ' '){                    degree[i] ++ ;                    degree[num] ++ ;                    sum ++ ;                    num = 0 ;                }                else {                    num = num * 10 + (in[j] - '0') ;                }            }            if(num){                degree[i] ++ ;                degree[num] ++ ;                sum ++ ;            }        }        cin >> in ;        int odd = 0 ;        int even = 0 ;        for (int i = 0 ; i < m ; i ++ ){            if(degree[i] & 1)odd ++ ;            else even ++ ;        }        if(!odd){            if(n == 0){                printf("YES %d\n",sum) ;            }            else {                puts("NO") ;            }        }        else {            if(odd == 2){                if((degree[n] & 1) && (degree[0] & 1) && (n != 0)){                    printf("YES %d\n" ,sum) ;                }                else {                    puts("NO") ;                }            }else {                puts("NO") ;            }        }    }    return 0 ;}

 

Related Article

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.