Euler circuit
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9797 Accepted Submission (s): 3554
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 INPUT3 3 3 sample OUTPUT10 Algorithm Analysis: This topic examines whether the Euler loop exists. You know, Euler's circuit is not the same as Euler's path. Euler's path exists, but Euler's path is not necessarily the presence of a Euclidean circuit! Because the Euler road is not necessarily a circuit, that is, as long as all the nodes can be connected together is a European road, but not necessarily smooth back to the starting point. A theoretical summary on the problem of Euler's path: 1 can you get out of a path from a single node without a graph, and each edge happens once, so that the route becomes a Euro-pull loop. "Just one Stroke" 2)
Euler's Road existence theorem: If an undirected graph is connected and has a maximum of 2 singularities, it must exist. (Singularity: Points with an odd number of nodes)If there are 2 singularities, then one must depart from one and the other singularity terminates. If the singularity does not exist, it can be started from any node and will eventually return to that point. 3) Directed graph theory: The premise: After ignoring the direction of the edge, the diagram must be connected, otherwise there can be no Euler road. There is a maximum of two nodes in the graph is not equal to the degree of the degree, and must be out of the degree of 1 of the node to do a starting point, into the degree of a large 1 of the node to do the end point. The code for this problem:
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include < Algorithm> #define N 1000+2using namespace Std;bool map[n][n];int odd[n];bool vis[n];int n;void dfs (int dd)//test diagram Connectivity { for (int i=1; i<=n; i++) {if (!vis[i] && map[dd][i]==true) {vis[i]=true; DFS (i); }}}int Main () {int m; int I, J; int DD; Count the number of singularities int u,v; while (scanf ("%d", &n)!=eof) {if (n==0) break; scanf ("%d", &m); For (i=0, i<=n; i++) {for (j=0; j<=n; J + +) {map[i][j]=false; }} memset (odd, 0, sizeof (odd)); for (i=0; i<m; i++) {scanf ("%d%d", &u, &v); Map[u][v]=true; Map[v][u]=true; odd[u]++; odd[v]++; } for (I=1; i<=n; i++) Vis[i]=false; int ans=0; Calculate the number of connected components for (I=1; i<=n; i++) {if (!vis[i]) {ans++; Vis[i]=true; DFS (i); }} if (Ans==1)//The number of connected components is 1, the description diagram is connected {dd=0; for (I=1; i<=n; i++) {if (odd[i]%2==1)//statistic singularity number dd++; } if (dd==0)//If the number of odd points is 0, then there is a Euler loop printf ("1\n"); else printf ("0\n"); } else printf ("0\n"); } return 0;}
HDU 1878 (1Y) (to determine if the Euler loop has a singularity number of 0 + one Unicom component * "template")