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", &A,&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)