Play on Words,uva 10129--Euler circuit/Euler pathway

Source: Internet
Author: User

Play on words


A question on the Purple book:

Enter n (n<=100000) words, whether all these words can be in a sequence, so that the first letter of each word is the same as the last letter of the previous word. Each word contains a maximum of 1000 lowercase letters. You can have duplicate words in the input.



Analysis:

Consider the letter as a knot, the word as a forward edge, then the problem has a solution, when and only if there is Ouratonlo/Euler circuit in the diagram

Ouratonlo/Loop: A path/loop that passes all the edges once and only once across all vertices in the diagram

There is a criterion for the existence of a Euler loop in a direction diagram: When and only if the graph is strongly connected and there is no singularity vertex, the penetration of all vertices is equal to the degree of

The existence of the Euler pathway for a directional graph: when and only if the graph is one-way connected, and just two odd vertices, one vertex in the degree of the degree of the first, the other vertex out of the degree of a freshman, the rest of the vertex is equal to the degree of the degree


Determining connectivity, Dfs,bfs,union-find Set


Note: Baidu on the use of DFS write code can not go through this example:

1

3

dc

Cb

Ba

Because unless the Euler loop can be an arbitrary starting point, if there is a singularity vertex, you must have two singularity vertices as the starting point and end point


The following is attached to the code of Baidu, the last of which is enclosed with the code to write the AC

#include <iostream> #include <cstring> #include <vector> #include <cstdio>using namespace std;    int map[26][26];int in[26],out[26];bool vis[26];int t,n;void dfs (int k) {vis[k]=true;    for (int i=0;i<26;i++) {if (Map[k][i]&&!vis[i]) DFS (i);    }}int Main () {scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Char c[1100];        memset (In,0,sizeof (in));        Memset (out,0,sizeof (out));        memset (map,0,sizeof (MAP));            for (int i=0;i<n;i++) {scanf ("%s", C);            int Len=strlen (C);            ++map[c[0]-' A '][c[len-1]-' a ';            ++out[c[0]-' a '];        ++in[c[len-1]-' a '];        } int a=0,b=0;        BOOL Flag=true;                for (int i=0;i<26;i++) {if (In[i]!=out[i]) {if (in[i]==out[i]+1) a++;                else if (in[i]+1==out[i]) b++;            else {flag=false;break;} }} if (A&AMP;&AMP;B&AMP;&AMP;A+B&GT;2) Flag=false;            if (flag) {memset (vis,false,sizeof (VIS));            for (int i=0;i<26;i++) if (Out[i]) {DFS (i);            BOOL Flag1=true;                for (int i=0;i<26;i++) {if (Out[i]&&!vis[i]) {flag1=false;break;}            if (In[i]&&!vis[i]) {flag1=false;break;}            } if (Flag1) {cout<< "ordering is possible.\n";        } else {cout<< "the door cannot be opened.\n";}        } else {cout<< "the door cannot be opened.\n"; }} return 0;}




And check the set:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define REP (i,n) for (int i=0;i< (n), ++i) #define for (I,A,B) for (int i=a;i<b;++i) #define MES (s,c) memset (s,c,sizeof (s)) using    namespace Std;int In[26],ou[26];char str[1010];int fa[26];int n;void Init () {mes (in,0), Mes (ou,0);    for (i,0,26) {fa[i]=i; }}int Find (int x) {return fa[x]==x? X:fa[x]=find (Fa[x]);}    BOOL Check_degree () {int num1=0,num2=0;            for (int i=0;i<26;++i) {if (In[i]!=ou[i]) {if (in[i]==ou[i]+1) num1++;            else if (in[i]+1==ou[i]) num2++;        else return false;    }} if (NUM1&AMP;&AMP;NUM2&AMP;&AMP;NUM1+NUM2&GT;2) return false; return true;}    BOOL Check_connected () {int cnt=0; for (i,0,26) {if (in[i]| |        Ou[i]) (&&fa[i]==i) {++cnt;    }}//cout<< "cnt=" <<cnt<<endl;    if (cnt!=1) return false; return true;} void Solve () {if (Check_degree ()) {if (Check_coNnected ()) printf ("Ordering is possible.\n");    else printf ("The door cannot be opened.\n");    } else{printf ("The door cannot be opened.\n");    }}int Main () {int T;    cin>>t;        while (t--) {Init ();        scanf ("%d", &n);            REP (i,n) {scanf ("%s", str);            int L=strlen (str);            int x=str[0]-' a ';            int y=str[l-1]-' a ';            in[y]++;            ou[x]++;        Fa[find (x)]=find (y);    } solve (); } return 0;}




Play on Words,uva 10129--Euler circuit/Euler pathway

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.