Hangzhou Electric acm1878--Euler circuit

Source: Internet
Author: User

Simple Euler circuit, title.

Euler's circuit judgment:

1. In the direction graph: The first necessary condition is that the graph is connected, so the vertex's penetration is equal to the degree of the.

2. In undirected graphs: the first condition or the graph is connected, followed by the vertex is even several (the degree of the vertex is even)

This problem is a graph, so according to the judgment method to write, very simple, the decision does not prove.

I use and check the set to determine whether the diagram is connected.

The following is the code for AC:

#include <iostream> #include <cstring>using namespace Std;int par[1005], Degree[1005];int finds (int x) {if (x = = Par[x]) return X;elsereturn par[x] = finds (Par[x]);} int main () {int A, b, N, M, I;while (CIN >> N) {if (n = = 0) break;for (i = 0; I <= N; i++) Par[i] = i;memset (degree, 0, sizeof (degree)); Cin >> m;for (i = 0; i < m; i++) {cin >> a >> b;degree[a]++;               The point degree +1degree[b]++;int x = finds (a), int y = finds (b), if (x! = y)                 //merge par[x] = y;} int flag = 0, Tag = 0;for (i = 1; I <= n; i++)           //whether connected if (par[i] = i) flag++;if (Flag > 1) cout << 0 << Endl;else{for (i = 1; I <= n; i++)  //The degree of vertex is even {if (Degree[i]% 2) tag++;} if (Tag > 0) cout << 0 << endl;elsecout << 1 << Endl;}} return 0;}


Hangzhou Electric acm1878--Euler circuit

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.