Euler circuit
Problem description is a loop that does not leave the pen on the 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?
The input test inputs contain 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 input knot
Beam.
Output takes one row for each test case, and outputs 1 if the Euler loop is present, otherwise outputs 0.
Sample Input
3 31 21 32 33 21 22 30
Sample Output
10
initial contact with Oralu and the euro-pull circuit:
Oralu and Euler circuit: Figure G, if there is a road, through the G each side has and only once, said the road is the European pull road, if there is a circuit through g each side have and only once,
This circuit is called the Euro-pull circuit. A diagram with a Euler loop becomes a Eulerian graph.
A method of judging whether Oralu exists
There is a graph: the graph is connected, there is a vertex out of the degree of 1, there is a vertex into the degree of 1, the rest is the degree of degrees = into.
undirected graph: The graph is connected, only two vertices are odd degrees, the rest are even degrees.
A method for judging the existence of Euler loops
To graph: The graph is connected, all the vertex degrees are in degrees.
undirected graphs: Graphs are connected, all vertices are even degrees.
and this problem I use DFS judgment, the code is as follows:
#include <cstdio> #include <cstring> #include <vector>using namespace Std;vector <int> f[1010]; int flag[1010],flag2[1010];//flag[i] Records the degree of the I Point, flag2[i] records whether I point traverse int n,m;void init () {//Initialize memset (flag,0,sizeof (flag)); memset (flag2,0,sizeof (Flag2)); for (int i=1;i<=n;i++) f[i].clear ();} void DFS (int point) {flag2[point]=1; for (int i=0;i<f[point].size (); i++) {int next=f[point][i]; if (!flag2[next]) DFS (next); }}int Main () {while (scanf ("%d", &n) ==1&&n) {scanf ("%d", &m); Init (); int A, B; for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); F[a].push_back (b); F[b].push_back (a); flag[a]++; flag[b]++; } bool Key1=true; for (int i=1;i<=n;i++) if (flag[i]%2) {key1=false; Break if (key1) {//has an odd degree, it does not exist. All is even, continue to judge bool Key2=true; DFS (1); for (int i=1i<=n;i++) if (!flag2[i]) {//If all traverse, then exists, and the other does not exist key2=false; Break } if (Key2) printf ("1\n"); else printf ("0\n"); } else printf ("0\n"); } return 0;}
also useful and check the set, but I do not have the system of learning ~ Others code as follows:
#include <stdio.h>using namespace Std;int pre[1007],dge[1007];int n,m;void init () {for (int i=1;i<=n;i++) { Pre[i]=i; dge[i]=0; }}int find (int x) {while (x!=pre[x]) x=pre[x]; return x;} void Unio (int i,int j) {/*int x=find (i); int Y=find (j); if (x==y) return; pre[x]=y;*/Pre[j]=find (i);} int main () {while (scanf ("%d", &n), N) {scanf ("%d", &m); Init (); int A, B; while (m--) {scanf ("%d%d", &a,&b); dge[a]++; dge[b]++; if (Find (a)!=find (b)) Unio (A, a); } int flag=0; for (int i=1;i<=n;i++) if (dge[i]%2) {printf ("0\n"); flag=1; Break } if (flag) continue; int x=pre[1]; for (int i=2;i<=n;i++) if (X!=find (i)) {flag=1; Break } if (flag) printf ("0\n"); else printf ("1\n"); } return 0;}
For questions about Euler's road, see blog: http://www.cnblogs.com/buptLizer/archive/2012/04/15/2450297.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1878. Euler circuit "Oralu and the first contact of the European pull circuit" "August 2"