Data Structure and algorithm problems Euler's loop

Source: Internet
Author: User

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

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.