ZOJ2016 POJ1386(有向圖的歐拉路判斷),zoj2016poj1386
Play on Words
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| 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 should 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]
題意:
題目描述:
有些秘門帶有一個有趣的詞迷。考古學家必須解開詞迷才能開啟門。由於沒有其他方法可以
開啟門,因此詞迷就變得很重要。
每個門上有許多磁碟。每個盤上有一個單詞,這些磁碟必須重新排列使得每個單詞第一個字
母跟前一個單詞最後一個字母相同。例如單詞"acm"可以跟在單詞"motorola"的後面。你的任務是
編寫一個程式,讀入一組單詞,然後判定是否可以經過重組使得每個單詞第一個字母跟前一個單
詞最後一個字母相同,這樣才能開啟門。
每個單詞只有首尾兩個字母很關鍵,並且每個單詞可以看成串連首尾兩個字母的一條有向邊(由首字母指向尾字母)。這樣每個測試資料中的一組單詞可以構造成一個圖:圖中的頂點為 26 個小寫字母,每個單詞為圖中的一條邊。構造好有向圖後,題目要判定是否可以經過重組使得每個單詞第一個字母跟前一個單詞最後一個字母相同,等效於判斷圖中是否存在一條路徑經過每條邊一次且僅一次,這就是有向歐拉通路。
#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;}