|
10129-play On words |
8881 |
24.43% |
1751 |
68.65% |
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 1070
Question type: Euler's Road
Question:
Some of the secret doors contain a very interesting word puzzle. the team of archaeologists has to solve it to open that doors. because there is no other way to open the doors, the puzzle is very important for us.
There is a large number of magnetic plates on every door. every plate has one word written on it. the plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. for example,
The word'' ACM''Can be followed by the word''MOtorola ''. your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule)
And consequently to open the door.
Translation:
There are some secret doors that contain very interesting word puzzles, and the team of archaeologists must solve them to open the door. Because there is no other way to even open these doors, it is very important to solve those questions.
There are many magnetic plates on each door with words on them. These plates must be moved to form a queue: in the queue, except for the first word, the beginning of each word and the end of the last word
Same. For example, you can connect ACM to the end of Motorola.
Your task is to write a program, read a series of words, and compute to determine whether they may be in such a queue.
Sample input:
32acmibm3acmmalformmouse2okok
Sample output:
The door cannot be opened.Ordering is possible.The door cannot be opened.
Analysis and Summary:
This is a typical Euler's road question.Euler's road DefinitionYes: Except the start and end points, the number of "incoming and outgoing" entries for other points should be equal. In other words, except the start and end points, the degrees of other points should be even.
For a directed graph, the outbound degree of one vertex must be exactly 1 greater than the inbound degree, and the inbound degree of the other vertex must be greater than the outbound degree.
If the number of singularity does not exist, you can start from any point and eventually return to the point (which becomes the Euler loop ).
The question provides a large number of words, but only the first and last letters are useful. Therefore, you only need to save the first and last letters.
Another key aspect of the Euler's road is to judge that this graph is connected, and there is only one connected branch.
This method can be used to query the set or be directly searched by DFS.
The following code provides two versions:
1. Euler's road + and query set
# Include <cstring> # include <iostream> # include <cstdio> # include <cmath> # define maxn 30 using namespace STD; int vis [maxn], n, T, f [maxn], rank [maxn], in [maxn], out [maxn]; void Init () // check the set to determine whether it is a connected graph {for (INT I = 0; I <maxn; I ++) f [I] = I, rank [I] = 0 ;} int find (int x) {int r = x; while (F [R]! = R) r = f [R]; F [x] = r; return r;} void Union (int x, int y) {x = find (X ); y = find (y); If (rank [x]> rank [y]) {f [y] = x ;} else {If (rank [x] = rank [y]) {rank [y] ++;} f [x] = y ;}} int main () {freopen ("input.txt", "r", stdin); scanf ("% d", & T); While (t --) {memset (in, 0, sizeof (in); memset (Out, 0, sizeof (out); char STR [1005]; Init (); scanf ("% d", & N ); for (INT I = 0; I <n; ++ I) {scanf ("% s", STR); + + out [STR [0]-' A']; ++ in [STR [strlen (STR)-1]-'a']; Union (STR [0]-'A', STR [strlen (STR) -1]-'A');} int CNT = 0; For (INT I = 0; I <27; ++ I) {If (in [I] | out [I]) & F [I] = I) ++ CNT;} bool flag = true; If (CNT! = 1) Flag = false; int num1 = 0, num2 = 0; For (INT I = 0; I <maxn; ++ I) {If (! Flag) break; If (in [I]! = Out [I]) {If (in [I] = out [I] + 1) {++ num1 ;} else if (out [I] = in [I] + 1) {++ num2 ;}else {flag = false; break ;}}} if (num1 & num2 & num1 + num2> 2) Flag = false; If (FLAG) {printf ("ordering is possible. \ n ");} else {printf (" The door cannot be opened. \ n ") ;}} return 0 ;}
2. Euler's road + DFS
#include<cstring>#include<iostream>#include<cstdio>#include<cmath>#define MAXN 100using namespace std;int vis[MAXN],G[MAXN][MAXN],N, T,f[MAXN],rank[MAXN], in[MAXN],out[MAXN];void dfs(int u){ vis[u] = true; for(int i=0; i<MAXN; ++i){ if(G[u][i] && !vis[i]) dfs(i); }}int main(){ freopen("input.txt","r",stdin); scanf("%d",&T); while(T--){ memset(G, 0, sizeof(G)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); char str[1005]; scanf("%d",&N); for(int i=0; i<N; ++i){ scanf("%s", str); ++G[str[0]-'a'][str[strlen(str)-1]-'a']; ++out[str[0]-'a']; ++in[str[strlen(str)-1]-'a']; } bool flag=true; int num1=0, num2=0; for(int i=0; i<MAXN; ++i){ if(!flag) break; if(in[i]!=out[i]){ if(in[i]==out[i]+1) { ++num1; } else if(out[i]==in[i]+1) { ++num2; } else {flag=false; break; } } } if(num1 && num2 && num1+num2>2) flag=false; if(flag){ memset(vis, 0, sizeof(vis)); for(int i=0; i<MAXN; ++i) if(out[i]) { dfs(i); break; } bool flag2=true; for(int i=0; i<MAXN; ++i){ if(in[i] && !vis[i]) { flag2=false; break; } if(out[i] && !vis[i]) { flag2=false; break; } } if(flag2) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } else{ printf("The door cannot be opened.\n"); } } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double