有向图欧拉通路的判定之hdu1116
来源:互联网 发布:济南泉方数据库 编辑:程序博客网 时间:2024/05/21 18:30
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3782 Accepted Submission(s): 1210
Problem 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.#include <iostream>#include <string.h>using namespace std;string s;int set[30];int vis[30];int chu[30];int ru[30];int p[30];int findx(int x){ int r =x; while(r!=set[r]) r=set[r]; return r;}void merge(int a,int b){ int fa=findx(a); int fb=findx(b); if(fa!=fb)set[fa]=set[fb];}int main(){ int t; cin>>t; while(t--) { int n; cin>>n; for(int i=0;i<27;i++) { set[i]=i; vis[i]=0; chu[i]=0; ru[i]=0; } int flg2=0; int cnt=0; for(int i=0;i<n;i++) { cin>>s; int a =(int) (s[0]-'a'); chu[a]++; int b = (int)(s[s.length()-1]-'a'); ru[b]++; merge(a,b); vis[a]=vis[b]=1; } for(int i=0;i<27;i++)//判断是否是连通的 { if(set[i]==i&&vis[i]){cnt++;} } int flg1=0; if(cnt==1)//连通 { for(int i=0,j=0;i<27;i++) { if(chu[i]!=ru[i]&&vis[i]) { flg1=1;//不能构成回路 flg2++; p[j++]=i;//保存入度与出度不相等的数据 } } if(flg1==0)//是回路 cout<<"Ordering is possible."<<endl; else //不是回路,但是连通图中只有两个端点的入度与出度不相等,并且起始点 //的出度-入度=1;终点的入度-出度=1,欧拉通路 if(flg2==2&&(chu[p[0]]-ru[p[0]]==1 && ru[p[1]]-chu[p[1]]==1 || chu[p[1]]-ru[p[1]]==1 && ru[p[0]]-chu[p[0]]==1 )) { cout<<"Ordering is possible."<<endl; } else cout<<"The door cannot be opened."<<endl; } else cout<<"The door cannot be opened."<<endl; }}
- 有向图欧拉通路的判定之hdu1116
- 有向图欧拉通路
- POJ 1386 有向图欧拉通路
- POJ 2337 Catenyms (有向图欧拉通路)
- POJ 2337 (有向图欧拉通路)
- POJ 1385 Play on Words (有向图欧拉通路)
- codeforces 508D Tanya and Password (有向图欧拉通路)
- POJ 1300 Door Man (有向图欧拉通路)
- 无向图欧拉通路
- hdu1116欧拉通路 回路
- 无向图欧拉回路的判定之hdu1878
- 有向图,无向图的欧拉回路和欧拉通路poj 2337
- POJ 2337 Catenyms(有向图欧拉路径判定&&打印)
- ZJU2016 Play on Words - 有向图的欧拉通路
- Strange Country II(暴力DFS判有向图的哈密顿通路)
- poj 2337 Catenyms(有向图的欧拉通路)
- NYOJ 单词拼接(求有向图的欧拉通路)
- 无向图欧拉回路的判定
- 关于UEditor在struts2下图片上传问题
- 数据库数据复制
- 编译goldfish内核后,emulator黑屏解决方法
- 主题模型-LDA小结
- 集合小总结
- 有向图欧拉通路的判定之hdu1116
- 总结scrollLeft,offsetLeft,clientLeft等属性
- hdu4530小Q系列故事——大笨钟
- 排序算法介绍
- python核心編程筆記
- 无向图欧拉回路的判定之hdu1878
- Android应用开发——Property Animation
- LeetCode: Binary Tree Inorder Traversal, Morris In Order Traversal
- 亲,你遇到this package depends on missing sdk platform 问题了吗