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)