-
Description:
-
Euler's loop refers to a loop that does not take a pen out of paper, but can draw each side of the graph only once and return to the starting point. Given a graph, I asked if there is an Euler loop?
-
Input:
-
The test input contains several test cases. The second row of each test case contains two positive integers: number of nodes N (1 <n <1st) and number of edges M. The subsequent m rows correspond to m edges, each row provides a positive integer that is the number of the two nodes that are directly connected to the edge (from 1 to n ). When N is 0, the input ends.
-
Output:
-
The output of each test case occupies one line. If the Euler loop exists, 1 is output; otherwise, 0 is output.
-
Sample input:
-
3 31 21 32 33 21 22 30
Sample output:
1
0
# Include <iostream> using namespace STD; const int max = 1001; int G [Max] [Max]; int du [Max]; bool visite [Max]; int N, m; bool judge () {for (INT I = 1; I <= N; I ++) {If (Du [I] % 2! = 0 | du [I] = 0) return false;} return true;} void DFS (INT start, Int & counts) {for (INT I = 1; I <= N; I ++) {If (G [start] [I] &! Visite [I]) {counts ++; visite [I] = true; DFS (I, counts) ;}} int main () {int S, E, counts; cin> N; while (n! = 0) {counts = 1; for (INT I = 0; I <= N; I ++) for (Int J = 0; j <= N; j ++) {G [I] [J] = 0 ;}for (INT I = 0; I <= N; I ++) {du [I] = 0; visite [I] = false;} CIN> m; For (INT I = 0; I <m; I ++) {CIN> S> E; G [s] [e] = 1; G [E] [s] = 1; Du [s] ++; Du [e] ++ ;} visite [1] = true; DFS (1, counts); If (counts = N & judge () cout <"1" <Endl; elsecout <"0" <Endl; CIN> N;} return 0 ;}
Data Structure and algorithm problems Euler's loop