poj 1386-Play on Words-并查集+欧拉
来源:互联网 发布:ubuntu nodejs 升级 编辑:程序博客网 时间:2024/05/16 00:49
//题意就是要是前一个的单词的尾字母和该单词的最后一个字母要是相同的话,就ok了(并查集)~
其实就是欧拉链
在这里明确一下欧拉回路:只存在欧拉通路,也就是欧拉链的图不一定是欧拉图,欧拉图是要包含欧拉回路的~
欧拉链就是一笔画问题,从起点出发,访问每一条边,有且仅有一次,顶点的访问次数可是是多次,它的特点就是
有且仅存在两个奇数点,分别是入度比出度大1和入度比出度少1
而欧拉回路是所有的点都是入度=出度的!!!
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<cstdio>#include<string>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;#define sf scanf#define pf printf#define clr(xx) memset(xx,0,sizeof(xx))#define INF 1000000001#define MIN -1000000001int parent[30];int in[30],out[30];bool visited[30];char s[1010];int i,x,y,n;int bingchaji(int x){return parent[x]==x?x:bingchaji(parent[x]);}void init(int fx,int fy){fx=bingchaji(x);fy=bingchaji(y);if(fx!=fy) parent[fx]=fy;}int main(){int cas;scanf("%d",&cas);while(cas--){clr(in);clr(out);clr(visited);scanf("%d",&n);gets(s);for(i=0;i<26;i++)parent[i]=i;for(i=1;i<=n;i++){scanf("%s",s);int len=strlen(s)-1;x=s[0]-'a';y=s[len]-'a';visited[x]=true;visited[y]=true;init(x,y);in[x]++;out[y]++;}for(i=0;i<26;i++)if(visited[i])break;x=bingchaji(i);for(i=i+1;i<26;i++)if(visited[i]&&x!=bingchaji(i))break;if(i<26){printf("The door cannot be opened.\n");continue;}int count1=0;int count2=0;for(i=0;i<26;i++)if(visited[i]){if(in[i]!=out[i]) count1++;if(in[i]-out[i]==1||out[i]-in[i]==1) count2++;}if(count1==0||(count1==2&&count2==2))printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}return 0;}
- 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(欧拉路径 + 并查集)
- 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 <欧拉道路+并查集>
- 【计划】完成一个WPF程序
- 将qt ui界面文件转换为pyqt python格式
- error C2440: 'static_cast'
- 按Win+E能直接打开TC么
- VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序未能启动”的问题
- poj 1386-Play on Words-并查集+欧拉
- android4.2 BlueTooth 协议发生改变
- open.taobao.com开放平台下php SDK 应用
- 使用 Float 类 节省开发时间
- 剑指offer-->二维数组中的查找
- hibernate配置文件的hibernate.hbm2ddl.auto
- Java BufferedReader
- Java加密框架
- 用jQuery实现ASP.NET GridView折叠伸展效果