Poj 1386 Euler Loop

Source: Internet
Author: User

The general idea is to give some words and ask if they can be concatenated into a string to connect words and letters at the beginning and end. For example, in the sample:
ACM
Malform
Mouse
We can construct:
ACM-> malform-> mouse, which meets the question requirements.
Obviously, we can construct a graph to solve this problem. If a letter is used as a node, words such as ACM may even have one edge. Similarly, for malform, we may even have one ring on M. At this time, you can also calculate the outbound and inbound degrees of points.
This is an Euler's path problem. As long as there is an Euler's circuit or Euler's path in the figure, it meets the requirements of the subject. The following theorem is used to determine whether a graph has an Euler or Euler Loop:

The Theorem directed graph G is Euclidean. if and only when the base graph of G is connected, the inbound degrees of all vertices are equal to the outbound degrees.
It is inferred that directed graph G is semi-Euclidean. if and only when the base graph of G is connected, and the vertex u has a higher inbound degree than the outbound degree 1 and V has a lower inbound degree than the outbound degree 1, the inbound level of all other vertices is equal to the outbound level.
You can use DFS to determine the graph connectivity.

Therefore, we must first use DFS to determine whether the entire graph is connected. If the graph is not connected, it will be useless. After the connectivity, scan the inbound degree again. Below isCode:

// 1386.cpp: defines console applications Program . // The idea should be clear so that the question can be made out. // Euler's loop # include "stdafx. H "# include <iostream >#include <cstring> using namespace STD; const int n = 26; typedef struct {int In, out;} graph; int V [N], linked; int G [N] [N]; void DFS (INT index) // locate the number of nodes. You can also use and query the set here. {// Determine whether the connection is successful. V [Index] = 1; for (INT I = 0; I <n; I ++) if (! V [I] & G [I] [Index]) DFS (I); linked ++;} int main () {int t, n, I, j; char cmd [1005]; graph degree [N]; scanf ("% d", & T); While (t --) {memset (degree, 0, sizeof (degree); memset (G, 0, sizeof (g); scanf ("% d", & N); While (n --) {int P1, P2; scanf ("% s", CMD); int Len = strlen (CMD); P1 = Command [0]-'A'; P2 = cmd [len-1]-'A '; degree [P1]. out ++; degree [P2]. in ++; G [P1] [P2] = G [P2] [P1] = 1;} memset (v, 0, sizeof (v); int first = 0, NP = 0; for (I = 0; I <n; I ++) if (degree [I]. in | Degree [I]. Out) {// If (! First) First = I; // you can get the first node here. It is not necessary to judge whether it is non-zero !!! NP ++;} linked = 0; DFS (first); If (linked! = NP) // number of nodes in the NP, and the linked is also. If they are equal, they are connected. {Cout <"The door cannot be opened. "<Endl; continue;} int NA = 0, NB = 0, Ne = 0, NT = 0; for (I = 0; I <n; I ++) {If (degree [I]. in = 0 & degree [I]. out = 0) continue; nt ++; If (degree [I]. in-degree [I]. out = 1) Na ++; else if (degree [I]. in-degree [I]. out =-1) Nb ++; else if (degree [I]. in = degree [I]. out) NE ++;} If (NA = 1 & nb = 1 & Ne = nt-2 | Ne = Nt) // is the condition of the Euler loop. Either it is all the same start and end letters, or the difference between the inbound and outbound degrees of the two nodes is one. Cout <"ordering is possible." <Endl; elsecout <"The door cannot be opened." <Endl ;}system ("pause"); 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.