poj1386 Play on Words (欧拉回路或欧拉路的判定)
2016-07-21 10:54
330 查看
Play on Words
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
InputThe 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. OutputYour 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 Input3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample OutputThe door cannot be opened.
Ordering is possible.
The door cannot be opened.
这道题判断是不是欧拉路,或者是欧拉通路
首先,这道题是有向图
关于有向图的判断:
欧拉回路:全部必须是偶数度节点才可以
欧拉通路(欧拉路):有2个节点,其中一个入度比出度多1,一个出度比入度多1,其余的
全部是偶数度节点。
当然,这个图必须联通才行。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11639 | Accepted: 3982 |
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 wordmotorola”. 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.
InputThe 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. OutputYour 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 Input3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample OutputThe door cannot be opened.
Ordering is possible.
The door cannot be opened.
这道题判断是不是欧拉路,或者是欧拉通路
首先,这道题是有向图
关于有向图的判断:
欧拉回路:全部必须是偶数度节点才可以
欧拉通路(欧拉路):有2个节点,其中一个入度比出度多1,一个出度比入度多1,其余的
全部是偶数度节点。
当然,这个图必须联通才行。
#include<iostream> using namespace std; #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<vector> const int maxn=1e5+5; const int maxm=1e3+5; #define read int Tcase;scanf("%d",&Tcase);while(Tcase--) #define cls(x,y) memset((x),(y),sizeof((x))); #define si(x) scanf("%d",&(x)) #define pb(x) push_back((x)) int n; char in[maxm]; vector<int> vec[26]; int ind[26]; int outd[26]; int all,tr; void dfs(int now){ int len=vec[now].size(); for(int j=0;j<len;++j){ int i=vec[now][j]; if(((all>>i)&1)&&(!((tr>>i)&1))){ tr|=(1<<i); dfs(i); } } } int main() { read{ si(n); cls(ind,0) cls(outd,0) for(int i=0;i<26;++i){ vec[i].clear(); } all=0; int s=-1; for(int i=0; i<n; ++i) { scanf("%s",in); int len=strlen(in); all|=(1<<(in[0]-'a')),all|=(1<<(in[len-1]-'a')); ++outd[in[0]-'a'],++ind[in[len-1]-'a']; vec[in[0]-'a'].pb(in[len-1]-'a'); vec[in[len-1]-'a'].pb(in[0]-'a'); s=(s==-1?(s=in[0]-'a'):s); } bool L=true; tr=0; dfs(s); for(int i=0;i<26;++i){ if(((all>>i)&1)&&(!((tr>>i)&1))){ L=false;break; } } int J=0,big=0,small=0; for(int i=0; i<26&&L; ++i) { if(ind[i]^outd[i]) { ++J; if(ind[i]==outd[i]-1)++big; else if(outd[i]==ind[i]-1)++small; } } if(L&&((J==2&&big==1&&small==1)||(!J))) { puts("Ordering is possible."); } else{ puts("The door cannot be opened."); } } return 0; }
相关文章推荐
- csdn 收藏夹
- linux 下c 编译和调试
- 史上最全Android Studio插件
- windows dos合并文件的两个命令 copy与 type(这个更好用 )
- linux环境下deb格式文件转换成rpm格式
- $(xx).load()同步
- R Tools for Visual Studio
- iOS 屏幕旋转
- C语言scanf函数用法详细解释!!!
- 第十章(P120):事业永远不会完结-------准备重启
- cocopods 报错 _OBJC_CLASS_
- iOS开发者账号、证书的区别以及证书被误删除
- 【Leetcode】121. Best Time to Buy and Sell Stock 【动态规划】
- js和Jquery获取选中select值和文本
- 页面加载时
- iOS数据库操作安全整理
- sql语法大全
- 设计
- 浅谈Python 字符串格式化输出(format/printf)
- 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法