hdoj 1116 Play on Words 【并查集】+【欧拉路】

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5622    Accepted Submission(s): 1850

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.

题意:给出几个字符串,如果一个字符串的首字符(尾子符)等于另外一个字符串的尾子符(首字符),就让他们连接起来,问最后能不能把所有的字符串都连接起来。

分析:很明显的是要用到并查集的只是,但是处理首尾字符的时候会有点麻烦,我们不妨将没一个字符的首尾字符都视为一个点,一个字符串就是一条边,那么该题就转化为了求边能不能形成一条连通图,之后就要用欧拉路来判断改图是否连通就好了。

注:欧拉路分为欧拉回路和欧拉通路;

欧拉通路:满足从一点出发经过每一条边且只经过一次,能把所有的边都经过的路

欧拉回路:欧拉通路并且最后回到原点的路;

如果是欧拉回路那么图中每个点的入读和处度都相等

如果是通路那么起始点的出度减入度为1, 终点处入度减出度为1。

代码:

/*hdoj 1116 并查集+欧拉通/回路*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 1005

int out[26], in[26], fat[26];
bool vis[26];
char s[M];

int f(int x){
	if(x != fat[x]) fat[x] = f(fat[x]);
	return fat[x];
}

void merge(int x, int y){
	int a = f(x);
	int b = f(y);
	if(a != b) fat[a] = b;
}

int main(){
	int n, t, i;
	scanf("%d", &t);
	while(t --){
		memset(vis, 0, sizeof(vis));
		memset(out, 0, sizeof(out));
		memset(in, 0, sizeof(in));
		scanf("%d", &n);
		for(i = 0; i < 26; i ++) fat[i] = i;
		for(i = 0; i < n; i ++){
			scanf("%s", s);
			int x = s[0]-'a';
			int y = s[strlen(s)-1]-'a';
			merge(x, y);
			++out[x]; ++in[y];
			vis[x] = vis[y] = 1;
		}
		int flag1 = 0;
		for(i = 0; i < 26; i ++){  //判断是否联连通
			if(vis[i]&&fat[i] == i) ++flag1;
		}
		if(flag1 > 1){
			printf("The door cannot be opened.\n"); continue;
		}
		int flag2, flag3;  //flag1是判断是否是全部出入度都相等,flag2是判读起始点有几个,flag3是终点有几个
		flag1 = flag2 = flag3 = 0;
		for(i = 0; i < 26; i ++){
			if(vis[i]&&out[i] != in[i]){
				++flag1;
				if(out[i]-in[i] == 1) ++flag2;
				if(in[i] - out[i] == 1) ++flag3;
			}
		}
		if(flag1 == 0) printf("Ordering is possible.\n");
		else if(flag1 == 2&&flag2 == 1&&flag3 == 1) printf("Ordering is possible.\n");
		else printf("The door cannot be opened.\n");
	}
	return 0;
} 
时间: 2024-10-13 10:45:30

hdoj 1116 Play on Words 【并查集】+【欧拉路】的相关文章

HDU5883 The Best Path(并查集+欧拉路)

题意: n个点m条边,问m条边构成的是否为欧拉路. 是的话输出路径上所有点的异或和,每个点经过几次异或几次. 思路: 先用并查集判断是否连通,然后如果是欧拉路的话有两种情况 如果奇数度节点有2个,就枚举这两个点做起点,选大的 如果都为偶数度节点,就枚举n个起点,选大的 /* *********************************************** Author :devil ************************************************ *

POJ-2513 Colored Sticks(字典树+并查集+欧拉)

题目链接:Colored Sticks 一道3个知识点结合的题目,可以说单个知识点的题目,都会做,一旦知识点结合起来,题目就不简单了 思路:这个题开始看就知道是并查集,但是不好处理的不同种单词的统计,所以理所应当联想到字典树,上次做字典树的题目啸爷出的是统计相同单词数,这个题目和那个一样,把flag加个编号即可,再利用并查集. 1750ms  水过 #include <iostream> #include <cstdio> #include <cstdlib> #inc

BZOJ 1116 [POI2008]CLO(并查集)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1116 [题目大意] Byteotia城市有n个towns,m条双向roads.每条road连接两个不同的towns, 没有重复的road.你要把其中一些road变成单向边使得:每个town都有且只有一个入度 [题解] 我们发现当一个连通块边数大于等于其点数的时候就满足条件, 那么此题如果出现边数少于点数的连通块就不成立, 用并查集能够完成判断. [代码] #include <cstd

hdoj 2473 Junk-Mail Filter【并查集节点的删除】

Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7515    Accepted Submission(s): 2368 Problem Description Recognizing junk mails is a tough task. The method used here consists o

Play on Words HDU - 1116 (并查集 + 欧拉通路)

Play on Words HDU - 1116 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

hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4384    Accepted Submission(s): 1673 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

hdoj 1878 欧拉回路(无向图欧拉回路+并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 思路分析:该问题给定一个无向图,要求判断该无向图是否存在欧拉回路:无向图判断存在欧拉回路的两个必要条件:该无向图为连通图且所有的结点的度数为偶数: 代码如下: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAX_N = 1000 + 10

hdoj 3478 Catch(二分图判定+并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题即为判断该图是否为一个连通图且不为二分图: (1)二分图的性质:对于无向图G=(V, E),如果可以将图中的点划分为两个不相交的点集X与Y = V - X(V为点集),使得图中所有的边邻接的两个点分别存在集合X与集合Y中,则称该图G为二分图: (2) 二分图判定算法:二分图一种判定方法是给图中的每一

HDOJ 题目3367 Pseudoforest(并查集)

Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1932    Accepted Submission(s): 746 Problem Description In graph theory, a pseudoforest is an undirected graph in which every conne

hdoj 1232 畅通工程 【并查集】

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37403    Accepted Submission(s): 19808 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有