UVa10129 Play on Words 并查集判断连通+欧拉通路

10 篇文章 0 订阅
9 篇文章 0 订阅

                                                 10129 Play on Words
    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 N that indicates the number of
plates (1 <= N <= 100000). Then exactly N lines 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.



不管是无向图还是有向图,首先要保证图的连通性,用DFS判连通或者用并查集都可以

在欧拉通路中,除了起点和终点外,其他点的度数应该是偶数。

如果一个无向图是连通的,且最多只有两个奇点,则一定存在欧拉道路。如果有两个奇点,则必须从一个奇点出发,另一个奇点终止;

如果奇点不存在,则可以从任意点出发,最终一定会回到该点(欧拉回路)。

有向图类似:最多只能有两个点的入度不等于出度,而且必须是其中一个点的出度恰好比入度大1(把它作为起点),另一个的入度比出度大1(把它作为终点)。

当然,前提条件是:在忽略边的方向后,图必须是连通的



#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <set>

using namespace std;

const int maxn = 26, lim = 1000;
int T, N;
char s[lim + 10];
vector<int> G[maxn + 10], Gr[maxn + 10];
int f[maxn + 10];

int getf(int x) {
	return x == f[x] ? x : f[x] = getf(f[x]);
}

void Merge(int x, int y) {
	int u = getf(x), v = getf(y);
	if (u != v) {
		if (u < v) {
			f[v] = u;
		}
		else {
			f[u] = v;
		}
	}
}

int abs(int x) {
	return x < 0 ? -x : x;
}

int main()
{
	cin >> T;
	while (T--) {
		scanf("%d", &N);
		for (int i = 0; i < 26; i++) {
			G[i].clear();
			Gr[i].clear();
		}
		for (int i = 0; i < 26; i++) {
			f[i] = i;
		}
		set<int> ss; //用于去重保存节点
		for (int i = 0; i < N; i++) {
			scanf("%s", s);
			int lens = strlen(s);
			int pre = s[0] - 'a', nex = s[lens - 1] - 'a';
			ss.insert(pre);
			ss.insert(nex);
			Merge(pre, nex);
			G[pre].push_back(nex); //邻接表保存出度信息
			Gr[nex].push_back(pre); //逆邻接表保存入度信息
		}
		if (N == 1) {
			puts("Ordering is possible.");
			continue;
		}
		set<int> ans;
		for (set<int>::iterator i = ss.begin(); i != ss.end(); i++) {
			ans.insert(getf(*i));
		}
		//判断最后是不是只有一个集合,即是否连通
		if (ans.size() > 1) {
			puts("The door cannot be opened.");
			continue;
		}
		int cnt = 0, cnts = 0, cnte = 0;
		for (set<int>::iterator i = ss.begin(); i != ss.end(); i++) {
			int out = G[*i].size(), in = Gr[*i].size(); //debug
			if (in != out) {
				if (in - out == 1) {
					cnte++;
				}
				if (out - in == 1) {
					cnts++;
				}
				cnt++;
			}
		}

		//没有入度不等于出度的点
		if (cnt == 0) {
			puts("Ordering is possible.");
			continue;
		}
		
		if (cnt > 2 || cnts > 1 || cnte > 1 || cnts + cnte != cnt) { //debug
			puts("The door cannot be opened.");
		}
		else {
			puts("Ordering is possible.");
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值