On words~~ Euler path and Euler circuit of Hangzhou Electric Acm1116--play

Source: Internet
Author: User

This problem, compared to the previous problem, increases the Euler path solution. And the problem is a graph. The topic probably means that the idiom solitaire, can pick up even if can open the door, therefore must consider two kinds, one is the circuit, the other one is a path.

The first WR was due to the failure to consider the loop as a factor.

In the direction graph , the solution method of Euler circuit and Euler path:

1. The Euro-pull circuit:

First of all, of course, the graph is connected, followed by the degree of the vertex is equal to the degree.

2. Euler path:

First of all, the diagram is connected, then there is a vertex of the degree of 1, there is a vertex of the degree of 1, the other vertex is equal to the degree of degrees.

These are no longer proof. The connection of graphs can be judged or set by DFS, and the individual likes and checks the set.


The following is the AC code, with detailed comments:

#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int par[30], indegree[30]    , outdegree[30];                                Par for and check set, Indegree for the degree, Outdegree for the degree of bool vis[30]; The VIS for that vertex has been tagged int finds (int x)//and lookup function {int r = x;while (r! = Par[r]) R = Par[r];int i = x, j ; while (i! = r)//path Compression {j = Par[i];p Ar[i] = R;i = j;} return r;} void join (int x, int y)//and the merge function of the check set {int FX = finds (x); int fy = finds (y); if (FX! = FY) par[fy] = FX;}        int main () {int T, N, I;char str[1005];scanf ("%d", &t), while (t--) {memset (indegree, 0, sizeof (indegree));                       Initialize each array memset (outdegree, 0, sizeof (outdegree)), memset (Vis, false, sizeof (VIS)); for (i = 0; i <; i++) Initialize and check set par[i] = i;scanf ("%d", &n), while (n--) {scanf ("%s", str); int len = strlen (str); join (Str[0]-' a ', str[le   N-1]-' a ');           Merge occurrences of vertex indegree[str[len-1]-' a ']++; The corresponding penetration +1outdegree[str[0]-'A ']++;                 Corresponding out degree +1vis[str[0]-' a '] = true; Mark the vertex has occurred vis[str[len-1]-' a '] = true;}          int flag = 0, Tag = 0, a = 0, b = 0; Flag to determine whether the diagram is connected, tag is not equal to the number of vertices in the figure <span style= "White-space:pre" ></span>//a for the number of points in the degree of 1, B is the number of points with a large entry degree of 1 for (i = 0; i <; i++)//Contract Connectivity {if (Vis[i] && par[i] = = i) flag++;} if (flag > 1)//not connected, direct output {printf ("The door cannot be opened.\n");} Else{for (i = 0; i <; i++)//Find tag,a, number of B {if (Vis[i] && indegree[i]! = Outdegree[i]) {tag+ +;} if (Vis[i] && indegree[i]-outdegree[i] = = 1) a++;if (Vis[i] && outdegree[i]-indegree[i] = = 1) b++;}  if (tag = = 0)//tag = 0, there is a Euler loop printf ("Ordering is possible.\n"); else if (a = = b && a = = 1 && tag = 2)//a = 1 && b = 1 && tag = 2. There is Euler path printf ("Ordering is possible.\n"); elseprintf ("The door cannot be opened.\n");}} return 0;}


On words~~ Euler path and Euler circuit of Hangzhou Electric Acm1116--play

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.