Graph theory of data structure experiment viii. the Euler loop judgment of the Euler Loop's non-direction graph

Source: Internet
Author: User
graph of data structure experiment Viii.: Euler loop Time limit:1000ms Memory limit:65536k Topic Description

In a park in Fort Konigsberg, there are seven bridges linking two islands and islands of the Pregel River to the river bank.



Can walk through such seven bridges, and only walk once per bridge. Euler, the Swiss mathematician, finally solved the problem and created topology. Through the study of the seven bridge problem, Euler not only answered the seven bridges of Konigsberg, but also proved the more extensive three conclusions about strokes, which people usually call Euler theorem. For a connected graph, it is usually the route from a node to the path that passes through is called Euler Road. People usually put a stroke into the Euler road to the starting point is called the Euclidean circuit. The diagram with the Euler loop is called Eulerian.

Your task is to determine whether or not a given set of graph data is Eulerian. Enter the continuous T-group data input, the first row of each set of data gives two positive integers, represents the number of nodes N (1 < N <= 1000) and the number of edges m, then the M row corresponds to the M bar, each row gives two positive integers, representing the number of two nodes connected by the edge, respectively,  The node is numbered from the 1~n. output is 1 Eulerian output, otherwise output 0. Sample Input

1
6
1 2
2 3
3 1 4 5
5 6 6 4 1 4 1 6 3 4 3-6
Sample Output
1
TipsIf the undirected graph is connected and the degrees of all nodes are even, there is a Euler loop, otherwise it does not exist. Source

#include <bits/stdc++.h> using namespace std;
int n,m;
int bin[1020]; struct node {int u,v;}
EDGE[1000000];
int dd[2000];
    int find (int x) {return bin[x]==x?x:bin[x]=find (bin[x]);} int Union (int x,int y) {int fx=find (x);
    int Fy=find (y);
if (fx!=fy) bin[fx]=fy;
    int Judge () {int u,v;
    for (int i=1;i<=n;i++) bin[i]=i;
        for (int i=0;i<m;i++) {u=edge[i].u;v=edge[i].v;
        if (Find (U)!=find (v));
    Union (U,V);
    int F=find (1);
    for (int i=1;i<=n;i++) {if (Find (i)!=f) return 0;
return 1;
    int main () {int T;
            while (~SCANF ("%d", &t)) {while (t--) {scanf ("%d%d", &n,&m);
           memset (dd,0,sizeof (DD));
            Memset (id,0,sizeof (id));
            int num=0;
                for (int i=0;i<m;i++) {int u,v;
                scanf ("%d%d", &u,&v);
               dd[u]++; dd[v]++;
                Edge[num].u=u;edge[num++].v=v;
            Edge[num].u=v;edge[num++].v=u;
            int flag=0;
                    for (int i=1;i<=n;i++) {if (dd[i]%2!=0) {flag=1;
                Break
            } if (flag) printf ("0\n");
               else {if (Judge ()) printf ("1\n");
            else printf ("0\n");
}} return 0;
 }


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.