Hangzhou Electric 1116--play on Words (Euler circuit + and check set)

Source: Internet
Author: User

Test instructions: Similar to the idiom solitaire, but it may be connected to the end.

Defined:
Euler circuit: Each side happens to go only once, and can return to the starting point of the path
Euler path: passes through each edge once, but does not require a return to the starting point

Euler circuit:--------no graph--each node has an even number of degrees.

--Directed graph--single direction, each point in the degree = = out.

--Mixed graph--don't know for a moment.

Euler path:

--No map--a non-direction diagram exists in the Euler loop, when and only if all vertices have an even number of degrees | | In addition to two degrees for odd numbers the rest are even.

--a direction graph--a forward graph exists Euler path, when and only if all vertices of the graph have a degree of zero or one vertex with a degree of 1 and the other with a degree of-1, the other vertex has a degree of 0.

--Mixed graph--lue.

Solve the Euler path.

#include <cmath>#include<cstdio>#include<cstring>#include<iostream>#defineN 27using namespacestd;intP[n],inch[N], out[N], vis[n], father[n];voidinit () { for(inti =0; i < N; i++) Father[i]=i; } intFind (inta) {    if(A = =Father[a])returnA; Else        returnFather[a] =Find (Father[a]); } voidMercy (intAintb) {    intQ =Find (a); intP =Find (b); if(Q! =P) Father[q]=P;}intMain () {intT; scanf ("%d", &T);  while(t--)    {        intNstrings; scanf ("%d", &N);        Init (); memset (inch,0,sizeof(inch)); memset (Vis,0,sizeof(VIS)); memset ( out,0,sizeof( out));  for(inti =1; I <= N; i++) {cin>>s; intx, y; X= s[0]-'a'; Y= S[s.length ()-1]-'a';  out[x]++;inch[y]++;            Mercy (x, y); VIS[X]= Vis[y] =1; }        intQ =0;  for(inti =0; i < N; i++)            if(Vis[i] && i==Father[i]) Q++; if(Q! =1)//determine whether connectivity;{printf ("The door cannot be opened.\n"); Continue; }        intK =0;  for(inti =0; i < N; i++)            if(Vis[i] &&inch[i]! = out[i]) p[k++] =i; if(! K//looping;{printf ("Ordering is possible.\n"); Continue; }        if(k==2&& ( out[p[0]]-inch[p[0]]==1&&inch[p[1]]- out[p[1]]==1||inch[p[0]]- out[p[0]]==1&& out[p[1]]-inch[p[1]]==1))//Euler path judgment;{printf ("Ordering is possible.\n"); Continue; } printf ("The door cannot be opened.\n"); }    return 0;}

Hangzhou Electric 1116--play on Words (Euler circuit + and check set)

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.