POJ 1386 Play on Words
2014-09-11 11:31
393 查看
Play on Words
Time Limit: 1000msMemory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1386
64-bit integer IO format: %lld Java class name: Main
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
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.".
Sample Input
3 2 acm ibm 3 acm malform mouse 2 ok ok
Sample Output
The door cannot be opened. Ordering is possible. The door cannot be opened.
Source
Central Europe 1999解题:判断图是否可以欧拉巡游。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 30; int uf[maxn],in[maxn],out[maxn]; int Find(int x){ if(x == uf[x]) return uf[x]; return uf[x] = Find(uf[x]); } void Union(int x,int y){ int tx = Find(x); int ty = Find(y); if(tx != ty) uf[tx] = ty; } int main() { int t,n,cnt,innum,outnum,len; char str[1010]; scanf("%d",&t); while(t--){ scanf("%d",&n); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i = 0; i < maxn; i++) uf[i] = i; for(int i = 0; i < n; i++){ scanf("%s",str); len = strlen(str)-1; out[str[0]-'a']++; in[str[len]-'a']++; Union(str[0]-'a',str[len]-'a'); } for(int i = cnt = 0; i < 27; i++) if((in[i]||out[i])&&uf[i] == i) cnt++; if(cnt != 1) {puts("The door cannot be opened.");continue;} bool flag = true; innum = outnum = 0; for(int i = 0; i < maxn; i++){ if(in[i] == out[i]) continue; if(in[i] - out[i] == 1) outnum++; else if(out[i] - in[i] == 1) innum++; else {flag = false;break;} } if(flag && (innum == 1 && outnum == 1 || innum == 0 && outnum == 0)) puts("Ordering is possible."); else puts("The door cannot be opened."); } return 0; }
View Code
相关文章推荐
- poj 1386 Play on Words (欧拉路判断)
- POJ 1386 Play on Words(欧拉路经典题)
- zoj 2016 poj 1386 Play on Words 欧拉回路+DFS
- POJ 1386 Play on Words
- poj 1386 Play on Words 【图论-欧拉路】
- Play on Words POJ - 1386 Euler路
- POJ 1386 Play On Words 【并查集】
- POJ 1386 Play on Words
- poj1386 Play on Words (欧拉回路)
- POJ 1386 Play on Words
- zoj 2016 && poj 1386 && uva 10129 Play on Words
- POJ 1386 Play on Words
- poj1386 Play on Words【欧拉图】
- poj1386 Play on words
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- zoj 2016 || poj 1386 Play on Words
- poj 1386 Play on Words
- poj Play on Words(1386)
- poj 1386 Play on Words
- poj 1386 Play on Words