hdu 1116 Play on Words(欧拉通路)

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.
 

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

 先将头尾转换为数字。算出各个点的入出度数。标记0-26哪些点有出现。

 判断是否为欧拉通路。1、根<=1 

                    2、有0个入出度数不同  ||  2个入出度数不同且相差1

其余全部不是欧拉通路。

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 1006
23 #define inf 1e12
24 int n;
25 char s[N];
26 int a[36];
27 int b[36];
28 int vis[36],fa[36];
29 void init(){
30    for(int i=0;i<33;i++){
31       fa[i]=i;
32    }
33 }
34 int find(int x){
35    return fa[x]==x?x:fa[x]=find(fa[x]);
36 }
37 void merge(int x,int y){
38    int root1=find(x);
39    int root2=find(y);
40    if(root1==root2) return;
41    fa[root1]=root2;
42 }
43 int main()
44 {
45    int t;
46    scanf("%d",&t);
47    while(t--){
48       init();//尼玛又给忘了。。。
49       memset(a,0,sizeof(a));
50       memset(b,0,sizeof(b));
51       memset(vis,0,sizeof(vis));
52       scanf("%d",&n);
53       for(int i=0;i<n;i++){
54          scanf("%s",s);
55          int len=strlen(s);
56          int num1=s[0]-‘a‘;
57          int num2=s[len-1]-‘a‘;
58          merge(num1,num2);
59          b[num1]++;
60          a[num2]++;
61          vis[num1]=1;
62          vis[num2]=1;
63       }
64       int cnt=0;
65       for(int i=0;i<26;i++){
66          if(find(i)==i && vis[i]){
67             cnt++;
68          }
69       }
70       if(cnt>1){
71          printf("The door cannot be opened.\n");
72          continue;
73       }
74       int tmp=0;
75       int p[30];
76       for(int i=0;i<26;i++){
77          if(a[i]!=b[i] && vis[i]){
78             p[tmp]=i;
79             tmp++;
80          }
81       }
82       if(tmp==0){
83          printf("Ordering is possible.\n");
84          continue;
85       }
86       if(tmp==2 && (a[p[0]]-b[p[0]]==1 && b[p[1]]-a[p[1]]==1 || b[p[0]]-a[p[0]]==1 && a[p[1]]-b[p[1]]==1)){
87             printf("Ordering is possible.\n");
88             continue;
89       }
90       printf("The door cannot be opened.\n");
91    }
92     return 0;
93 }

时间: 2024-08-24 17:54:29

hdu 1116 Play on Words(欧拉通路)的相关文章

HDU 5883 F - The Best Path 欧拉通路 &amp; 欧拉回路

给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为是回路,所以你的开始点就会被访问多一次,所以如果是欧拉回路的话,还需要O(n)扫一次,枚举每个点作为起点. 欧拉通路的话,结果是固定的,因为只能从奇数度小的那个点作为起点,奇数度大的那个点作为终点. 关于点的访问次数:anstime  = Degree[i] / 2; //如果是奇数的,还要加上一.

关于欧拉通路、欧拉回路的一些定理,推论

关于欧拉通路.欧拉回路的一些定义: 无向图:G是一个连通的无向图(1)经过G的每条边一次并且仅一次的路径为欧拉通路(起点和终点不一定要一样).(2)如果欧拉通路是回路(起点和终点是同一个),则为欧拉回路.(3)具有欧拉回路的无向图G称为欧拉图. 有向图:D是一个有向图,D的基图(把D的有向边改为无向边)是连通的(1)经过D的每条边一次并且仅一次的路径称为有向欧拉通路(起点和终点不一定一样).(2)如果有向欧拉通路是回路(起点和终点一样),那么称为有向欧拉通路.(3)具有有向欧拉通路的有向图D称为

图论——欧拉通路、欧拉回路(有向图无向图混合图)

之前稍微了解有向图.无向图.混合图的欧拉通路.欧拉回路,这里做下笔记,以便日后翻阅. 无向图: 存在欧拉回路的条件:原图连通,每个结点均为偶度结点. 存在欧拉通路的条件:存在欧拉回路,或原图连通,有两个结点为奇度结点,其他结点均为偶度结点. 有向图: 存在欧拉回路的条件:基图连通,每个结点的入度等于出度. 存在欧拉通路的条件:存在欧拉回路,或基图连通,有一个结点入度等于出度+1,有一个结点出度等于入度+1,其他结点的入度等于出度. 混合图: 存在欧拉回路的条件: 1.将无向边随便定向,每个结点的

POJ 1386 Play on Words(有向欧拉通路 连通图)

题意  见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边  然后就是输入m条边  推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和入度相等  或者  仅仅有两个入度和出度不相等的点  且这两点入度与出度的差一个为-1(起点)一个为1(终点). 推断是否连通就是应用并查集了 #include<cstdio> #include<cstring> using namespace std; const int N = 3

poj 2513 Colored Sticks(欧拉通路+并查集+字典树)

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 35612   Accepted: 9324 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

poj2513Colored Sticks(欧拉通路+字典树+并查集)

Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color? Input Input is a

POJ 2513 无向欧拉通路+字典树+并查集

题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧拉通路的判断,但是map效率太低,超时了 网上看了一遍发现必须得用效率更高的字典树对每个不同的颜色进行赋值 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace st

一笔画问题 南阳oj42 【并查集+欧拉通路的判断】

描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数N(N<=10)表示测试数据的组数. 每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线.(点的编号从1到P) 随后的Q行,每行有两个正整数A,B(0<A,B<P),表示编号为A和B的两点之间有连线. 输出 如果存在符合条件的连线

ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define MAX 1005 #define MAXS 24 //姓名 #define MAXN 26 //字母 struct Edge{ char name