hdu1878-Euler circuit (no direction diagram exists in the Euler circuit, entry question)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1878

" Concept "

Euler circuit: If there is such a path in Figure g, so that it passes through each edge of the G once, it is said that the path is a Eulerian path . If the path is a circle, it is called a Euler (Euler) loop .

The topic is an no-show diagram:

the necessary and sufficient conditions for the existence of the Euler circuit in the non-graphAn undirected graph exists in the Euler loop, when and only if all vertices in the graph have an even number of degrees, and the graph is a connected graph.

The concept of degrees is used in this diagram:

The degree of a,b,c,d,e is the degree of 3,f, which is 5, which is a few degrees from the edge of the point.

Odd number of edges for the singularity, even the number of edges for even degrees, the vertex degree is all the singularity, so there is no Euler circuit, that is, cannot " a stroke ."

Determine whether to connect with and check set judgment can be;

Code

#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int degree[1200];int n,m; int num[1200];void Connect (int a,int b) {//and check set <span style= "font-family:arial;" > Connect </span>if (num[a]==num[b]) return, int p=num[a];int q=num[b];for (int i=1;i<=n;i++) {if (num[i]=p) num[i] =q;}} int main () {while (cin>>n&&n) {cin>>m;for (int i=1;i<=n;i++)//<span style= "font-family: Arial; " > Initialize </span>num[i]=i;memset (degree,0,sizeof (degree)) for the set, and for (int i=0;i<m;i++) {int a,b;scanf ("%d%d", &AMP;A,&AMP;B);d egree[a]++;//degree <span style= "font-family:arial;" > Plus 1</span>degree[b]++;connect (A, b);} int Sign=0;<span style= "font-family:arial;" >//is used to mark whether all degrees are even </span>for (int i=1;i<=n;i++) {if (degree[i]%2) {sign=1;break;}} if (sign) cout<< "0" <<endl;else{int cnt=0;for (int i=1;i<=n;i++)//<span style= "font-family:arial;" > Determine if Unicom </span>{if (num[i]==i) cnt++;} if (cnt==1) cout<< "1" <<endl;elsecout<< "0"<<endl;}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1878-Euler circuit (no direction diagram exists in the Euler circuit, entry question)

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.