POJ 1386 Play on Words (有向图欧拉路径判定)

摘要:
PlayonWordsTimeLimit:10000MS内存限制:10000K总提交量:8768接受:3065描述某些密码门包含一个有趣的密码谜。考古专家团队必须打开这些门
Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8768 Accepted: 3065

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. 

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

用并查集判断连通,然后判断欧拉路径存在。

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2014-2-3 14:18:41
 4 File Name     :E:2014ACM专题学习图论欧拉路有向图POJ1386.cpp
 5 ************************************************ */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 
21 int F[30];
22 int find(int x)
23 {
24     if(F[x] == -1)return x;
25     else return F[x] = find(F[x]);
26 }
27 void bing(int u,int v)
28 {
29     int t1 = find(u);
30     int t2 = find(v);
31     if(t1 != t2)F[t1] = t2;
32 }
33 char str[1010];
34 int in[30],out[30];
35 int main()
36 {
37     //freopen("in.txt","r",stdin);
38     //freopen("out.txt","w",stdout);
39     int T;
40     int n;
41     scanf("%d",&T);
42     while(T--)
43     {
44         scanf("%d",&n);
45         memset(F,-1,sizeof(F));
46         memset(in,0,sizeof(in));
47         memset(out,0,sizeof(out));
48         int s = -1;
49         while(n--)
50         {
51             scanf("%s",str);
52             int len = strlen(str);
53             int u = str[0] - 'a';
54             int v = str[len-1] - 'a';
55             bing(u,v);
56             out[u]++;in[v]++;
57             if(s == -1)s = u;
58         }
59         bool flag = true;
60         int cc1 = 0,cc2 = 0;
61         for(int i = 0;i < 26;i++)
62         {
63             if(out[i] - in[i] == 1)cc1++;
64             else if(out[i] - in[i] == -1) cc2++;
65             else if(out[i] != in[i])
66                 flag = false;
67             if(out[i] || in[i])
68                 if(find(i) != find(s))
69                     flag = false;
70         }
71         if( !( (cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1) ) )flag = false;
72         if(flag)printf("Ordering is possible.
");
73         else printf("The door cannot be opened.
");
74     }
75     return 0;
76 }

免责声明:文章转载自《POJ 1386 Play on Words (有向图欧拉路径判定)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java学习资源整理(超级全面)nginx匹配规则说明以及匹配的优先级下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue教程:组件Component详解(六)

一.什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装可重用的代码。在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能。在有些情况下,组件也可以表现为用 is 特性进行了扩展的原生 HTML 元素。 所有的 Vue 组件同时也都是 Vue 的实例,所以可接受相同的选项对象...

Mac版sublime text右键open in browser 不能识别中文名解决办法

问题描述: Mac下sublime text下打开中文命名的html文件,右键open in browser,浏览器无反应。 解决思路: 要么适应软件,要么改进软件来适应。 1.  将中文名的html文件,改成英文名的html文件来预览。 2.  改造sublime text, 安装插件SideBarEnhancements,使用插件的“open in...

android可拖动排序GridView实现

经常使用今日头条、网易新闻的同学们应该都会注意到用于管理多个频道的可拖动排序GridView,下面介绍一下可拖动的DragGridView的实现方法。代码放在GitHub上https://github.com/zhaoyu87/DragGridView,需要的同学可以下载 DragGridView继承自GridView,当长按选中某个item进行拖动,放...

C# 如何获取Url的host以及是否是http

参考资料:https://sites.google.com/site/netcorenote/asp-net-core/get-scheme-url-host Example there's an given url: http://localhost:4800/account/login 获取整个url地址: 在页面(cstml)中  Microsoft...

HTML5新特性之文字转语音

/** * 文字转语音,利用H5的新特性SpeechSynthesisUtterance,speechSynthesis实现 * eg. * const speaker = new Speaker({ text: '这是一条神奇的天路啊' }); * speaker.start(); // 开时播放 * setTimeout((...

CentOS7安装桌面环境 并支持远程访问

最近要在CentOS环境下装IDE,之前的CentOS都是做服务器用,都是最小安装,没有桌面环境,因此需要……搞快点! 1、安装所需软件 yum install -y epel-release yum install -y lightdm yum groups mark convert "X Window system" yum groups mark...