Play on words
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:9710 |
|
Accepted:3352 |
Description 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.
Input The input consists of T test cases. the number of them (t) is given on the first line of the input file. each test case begins with a line containing a single integer number nthat indicates the number of plates (1 <= n <= 100000 ). then exactly nlines follow, each containing a single word. each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'Z' will appear in the word. the same word may appear several times in the list.Output Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. all the plates from the list must be used, each exactly once. the words mentioned several times must be used that number of times. If there exists such an ordering of plates, your program shocould print the sentence "ordering is possible.". Otherwise, output the sentence "The door cannot be opened .".
Sample Input 32acmibm3acmmalformmouse2okok Sample output The door cannot be opened.Ordering is possible.The door cannot be opened. Source Central Europe 1999 |
[Submit] [Go Back] [Status] [discuss]
Question:
Description:
Some secrets contain an interesting word fan. Archaeologists can open the door only by unlocking the word fans. There is no other method.
Open the door, so fans become very important.
Each door has many disks. Each disk has a word. These disks must be rearranged to make each word the first word.
The last letter of the parent word is the same. For example, the word "ACM" can be followed by the word "Motorola. Your task is
Write a program, read a group of words, and then determine whether the first letter of each word can be reorganized before a single
Only when the last letter of a word is the same can the door be opened.
Each word has only two letters at the beginning and end, and each word can be regarded as a directed edge connecting the first and last two letters (from the first letter to the last letter ). In this way, a group of words in each test data can be constructed into a graph: the vertex in the graph is 26 lowercase letters, and each word is an edge in the graph. After constructing a directed graph, you need to determine whether the first letter of each word can be restructured to make it the same as the last letter of the previous word, it is equivalent to determining whether a path passes through each edge once and only once in the graph. This is the directed Euler path.
#include <cstdio>#include <cstring>#include<vector>#include<iostream>using namespace std;#define MAXN 100001#define INF 100000000char word[MAXN];const int sz=30;int p[sz],v[sz],in[sz],out[sz],E;int G[sz][sz];int findx(int x){return p[x]==x?x:p[x]=findx(p[x]);}void Union(int u,int v){p[findx(u)]=findx(v);}bool is_connect(){int x=0;for(int i=0;i<26;i++)if(v[i]){x=i;for(int j=0;j<26;++j)if(G[i][j]){Union(i,j);}}x=findx(x);for(int i=0;i<26;i++)if(v[i]){if(x!=findx(i))return false;}return true;}void AddEdge(int from,int to){v[from]=v[to]=1;if(from==to)return;G[from][to]=1;out[from]++;in[to]++;}void Init(){memset(in,0,sizeof in);memset(out,0,sizeof out);memset(v,0,sizeof v);memset(G,0,sizeof G);for(int i=0;i<26;i++)p[i]=i;}int main(int argc, char const *argv[]){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);bool ok=true;Init();while(n--){scanf("%s",word);AddEdge(word[0]-'a',word[strlen(word)-1]-'a');}int x1=0,x2=0;for(int i=0;i<26;i++)if(v[i]){if(out[i]-in[i]>1||in[i]-out[i]>1){ok=false;break;}else if(out[i]-in[i]==1){x1++;if(x1>1){ok=false;break;}}else if(out[i]+1==in[i]){x2++;if(x2>1){ok=false;break;}}}if(x1!=x2){ok=false;}if(ok&&!is_connect()) ok=false;puts(ok?"Ordering is possible.":"The door cannot be opened.");}return 0;}
Zoj2016 poj1386 (Euler's path judgment for Directed Graphs)