例题6-16 单词 并查集判断连通+有向图欧拉路规律
2015-09-27 21:01
375 查看
Play on Words
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 Specification
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 Specification
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 wordsmentioned 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
3 2 acm ibm 3 acm malform mouse 2 ok ok
Output for the Sample Input
The door cannot be opened. Ordering is possible. The door cannot be opened.
分析:
把首尾字母看做节点,单词看成是有向边,用并查集判断是否连通,然后再根据有向图的结论:最多只能有两个点的入读不等于出度,而且必须是其中一个点的出度比入度大一,另一个点的入度比出度大一。
#include<iostream> #include<algorithm> #include<cstdio> #include<string> #include<cstring> using namespace std; const int maxn=100005; int n,head,tail; char s[1001]; int in[30],out[30],p[30]; int findfa(int x) { return p[x]==x?x:(p[x]=findfa(p[x])); } int main() { // freopen("f.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d",&n); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=0;i<26;i++) p[i]=i; for(int i=0;i<n;i++){ scanf("%s",s); head=s[0]-'a'; tail=s[strlen(s)-1]-'a'; in[tail]++; out[head]++; if(findfa(head)!=findfa(tail)) p[findfa(head)]=findfa(tail); } int k=0; bool flag=true; int flag1=0,flag2=0; // while(in[k++]==0); for(k=0;!in[k]&&!out[k];k++); for(int i=k+1;i<26;i++){ if((in[i]||out[i])&&findfa(i)!=findfa(k)){ flag=false; break; } } if(flag) for(int i=k;i<26;i++){ if(in[i]!=out[i]){ if(in[i]==out[i]+1){ flag1++; } else if(in[i]+1==out[i]) flag2++; else { flag=false; break; } } } if(!flag||flag1>1||flag2>1)cout<<"The door cannot be opened."<<endl; else cout<<"Ordering is possible."<<endl; } return 0; }
相关文章推荐
- 那些年想不起来的Git命令
- RC4加密算法
- UIView
- java SE复习笔记41
- 烦躁
- Devexpress汉化修改 已经汉化过后生成的*.resources.dll文件 z
- iOS6之后 NSAttributedString 福利
- 组合数(dfs)
- Java工具类(二) 模版导出工具类
- 软工视频总结(前期)
- Basic Calculator
- OCP-V13-008
- 两种方法实现stm32的8个串口通信
- jsonp的工作原理
- ASP.NET WebAPI 05 参数绑定
- HTML使用canvas载入图片
- 三个水杯 21 (bfs 转化) 好题
- 编译lamp
- 关于测试方法以及测试用例设计的一些总结
- HDU 5493 Queue (合肥网络赛 1010 )