[欧拉回路] poj 1386 Play on Words
来源:互联网 发布:唐山学院网络教育平台 编辑:程序博客网 时间:2024/05/21 20:29
题目链接:
http://poj.org/problem?id=1386
Play on Words
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9685 Accepted: 3344
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.
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.".
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]
题目意思:给n个单词,如果A单词的最后一个字母和B单词的第一个字母是一样的,则说明A和B能够连在一起。问所有单词是否连成一串。
解题思路:
判断连通性+欧拉通路
只考虑单词的首字母和末尾字母,把出现的字母当成节点,一个单词的首字母和尾字母连成一条边。先用并查集判断是否连通,然后判断是否有欧拉通路。
代码:
//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 30bool vis[Maxn];int dei[Maxn],deo[Maxn],fa[Maxn],n;char sa[1100];int Find(int x){ int temp=x; while(fa[x]!=x) x=fa[x]; while(fa[temp]!=x) { int cur=fa[temp]; fa[temp]=x; temp=cur; } return x;}void Unio(int x,int y){ x=Find(x),y=Find(y); if(x!=y) fa[x]=y;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(vis,false,sizeof(vis)); memset(dei,0,sizeof(dei)); memset(deo,0,sizeof(deo)); for(int i=1;i<=26;i++) fa[i]=i; while(n--) { scanf("%s",sa+1); int len=strlen(sa+1); deo[sa[1]-'a'+1]++; dei[sa[len]-'a'+1]++; vis[sa[1]-'a'+1]=true; vis[sa[len]-'a'+1]=true; Unio(sa[1]-'a'+1,sa[len]-'a'+1); } int la=-1,nui=0,nuo=0; bool ans=true; for(int i=1;i<=26;i++) { if(vis[i]) { if(la==-1) la=Find(i); else if(la!=Find(i)) { //printf("->i:%d la:%d\n",i,la); //system("pause"); ans=false; break; } if(dei[i]-deo[i]==1) nui++; else if(deo[i]-dei[i]==1) nuo++; else if(deo[i]!=dei[i]) { ans=false; break; } //printf("i:%d la:%d\n",i,la); //system("pause"); } } if(!ans||nui>=2||nuo>=2||(nui+nuo)==1) printf("The door cannot be opened.\n"); else printf("Ordering is possible.\n"); } return 0;}
0 0
- POJ Play on Words(欧拉回路)
- POJ 1386 Play on Words(判断欧拉回路存在)
- [欧拉回路] poj 1386 Play on Words
- POJ 1386 Play on Words 欧拉回路
- poj1386 Play on Words (欧拉回路)
- (欧拉回路)Play on Words(P1386)
- Play on Words UVa欧拉回路
- poj 1386 Play on Words(欧拉回路…
- POJ--1386--Play on Words【判断有向图欧拉通路、欧拉回路】
- HDU 1116 &&POJ 1386 Play on Words(欧拉回路)
- POj 1386 Play on words 欧拉回路/通路,图的连通性判断
- zoj 2016 poj 1386 Play on Words 欧拉回路+DFS
- poj 1386 Play on Words(欧拉回路&&并查集)(中等)
- HDU 1116 Play on Words(欧拉回路)
- HDU 1116 Play on Words (欧拉回路)
- zoj 2016 Play on Words 欧拉回路
- Play on Words uva DFS+欧拉回路
- hdu1116 Play on Words(典型欧拉回路)
- hdu 5045 费用流
- 【Manacher】 HDOJ 3068 最长回文
- JavaSE线程复习之路
- 输出一个集合的所有子集(算法)
- Storm基础知识
- [欧拉回路] poj 1386 Play on Words
- 靠谱的同步工具,记同步短信经历
- Note for video Machine Learning and Data Mining——The Learning Problem
- 蓝的成长记——追逐DBA(7):基础命令,地基之石
- IOS Layer的使用
- qt for android实现程序的开机自启动。
- zoj 2391 Beautiful People 最长上升子序列
- hdu1827——Summer Holiday
- Mac环境 go语言之入门HelloWorld