poj 1386 Play on Words(单向欧拉函数+并查集)
来源:互联网 发布:php蜘蛛池程序 编辑:程序博客网 时间:2024/06/05 00:17
题目链接:
http://poj.org/problem?id=1386
Play on Words
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10871 Accepted: 3709
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.题目大意就是给你n个单词问你这n个单词能否首尾连接成一串,有点像成语接龙的感觉。。
思路就是用并查集判断是否联通,欧拉回路判断出度入度就好
AC代码:
v#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<utility>using namespace std;vector<pair<int ,int> >edge;int in[30],out[30];int set[30];int used[30];int n;void init(){ memset(used,0,sizeof(used)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=0;i<=26;i++) set[i]=i; edge.clear();}int find(int a){ int root=a; int temp; while(set[root]!=root) root=set[root]; while(set[a]!=root) { temp=a; a=set[a]; set[temp]=root; } return root;}bool merge(int a,int b){ int x=find(a); int y=find(b); if(x==y) return false; set[x]=y; return true;}bool eluer(){ int ort,st,ed; st=ed=0; ort=-1; for(int i=0;i<edge.size();i++) merge(find(edge[i].first),find(edge[i].second)); for(int i=0;i<26;i++) if(used[i]) { if(ort==-1) ort=i; if(abs(in[i]-out[i])>=2) return false; if(find(i)!=find(ort)) return false; if(in[i]-out[i]==1) { ed++; if(ed>1) return false; } if(out[i]-in[i]==1) { st++; if(st>1) return false; } } return true;}int main(){ char s[1100]; int t; scanf("%d",&t); for(int kk=0;kk<t;kk++) { scanf("%d",&n); init(); for(int i=0;i<n;i++) { scanf("%s",s); int u=s[0]-'a'; int v=s[strlen(s)-1]-'a'; in[v]++,out[u]++; used[v]=used[u]=1; edge.push_back(make_pair(v,u)); } if(!eluer()) printf("The door cannot be opened.\n"); else printf("Ordering is possible.\n"); } return 0;}
0 0
- poj 1386 Play on Words(单向欧拉函数+并查集)
- poj 1386-Play on Words-并查集+欧拉
- poj 1386 ZOJ 2016 Play on Words(并查集+欧拉通路)
- poj 1386 Play on Words(欧拉回路&&并查集)(中等)
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- HDU1116-Play On Words 并查集,欧拉回路
- hdoj 1116 Play on Words 【并查集+欧拉】
- Play on Words(欧拉回路+并查集)
- POJ 1386 Play On Words 【并查集】
- hdu 1116 Play on Words(并查集+欧拉回路|| 欧拉路径)
- HDOJ 1116-Play on Words【欧拉路径+欧拉回路+并查集】
- [HDU 1116]Play on Words(欧拉回路/欧拉路径+并查集)
- HDU 1116 Play on Words(并查集,欧拉回路)
- POJ1386 Play on Words 欧拉回路[并查集判断连通]
- Play on words 之并查集+欧拉回路解题报告
- hdu 1116 Play on Words 并查集+欧拉通路
- 【并查集 欧拉路径】hdu 1116 Play on Words
- UVA - 10129 - Play on Words <欧拉道路+并查集>
- (更新)OutputDebugString函数简单封装,实现格式化打印输出(VC++)
- java socket 长连接 nputStream的read 阻塞问题情况之一
- [sicily]1813. M进制数问题
- HDU - 1864 最大报销额(01背包)
- 汉诺塔
- poj 1386 Play on Words(单向欧拉函数+并查集)
- 后台程序
- Android Api Demos登顶之路(八十)Graphics-->PathFillTypes
- Mysql 锁机制详解
- rotate图片旋转 http://blog.csdn.net/lonelyroamer/article/details/8252533
- LeetCode---Linked List Cycle
- TI DSP的CMD文件
- hdu1141
- 备忘----android环境搭建