Topic 1027: Euler circuits

Source: Internet
Author: User

Title Description:
The
Euler circuit refers to a loop that does not leave the pen on paper, but only once per side of the picture and can return to the starting point. Now given a diagram, ask if there is a Euler circuit?
Input:
The
test input contains several test cases. The 1th line of each test case gives two positive integers, namely the number of nodes N (1 < N < 1000) and the number of sides m, followed by M-bars, each with a pair of positive integers, the number of two nodes (nodes from 1 to N) that are directly connected to the bar. When n is 0 o'clock, the input ends.
Output:
the output of each test case is one row, or output 1 if the Euler loop is present, otherwise output 0.
Sample input:
3 31 21 32 33 21 22 30
Sample output:
10

Code:
#include <iostream>using namespacestd;inttree[1001];//stores the parent node for each nodeintdegree[1001];//the degree to which each node is stored (no graph) intFindRoot (intx) {    if(tree[x]==-1)        returnx; Else{        inttmp=FindRoot (tree[x]); TREE[X]=tmp; returntmp; }} /*thought: 1. Use and check the set to determine whether it is connected Figure 2. If the given graph is a connected graph, then look at the degree of each node, if the degree of a node is 1, then the "Euler loop" will not be formed.*/ intMain () {intn,m;  while(cin>>N) {        if(n==0)             Break; CIN>>m;  for(intI=1; i<=n;++i) {//Initializetree[i]=-1; Degree[i]=0; }         for(intI=0; i<m;++i) {            intb; CIN>>a>>b; ++Degree[a]; ++Degree[b]; intRoot_a=FindRoot (a); introot_b=FindRoot (b); if(root_a!=root_b) {Tree[root_b]=root_a; }        }        intCnt=0;//Count the number of connected components        BOOLflag=true;  for(intI=1; i<=n;++i) {            if(tree[i]==-1)                ++CNT; }        if(cnt!=1) {cout<<0<<Endl; Flag=false; }        Else{             for(intI=1; i<=n;++i) {                if(degree[i]==1){//See if there is a node with a degree of 1cout<<0<<Endl; Flag=false;  Break; }            }        }        if(flag==true) cout<<1<<Endl; }    return 0;} /************************************************************** problem:1027 User:lcyvino language:c++ Re sult:accepted time:130 Ms memory:1528 kb****************************************************************/

Topic 1027: Euler circuits

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.