【图论05】并查集 1001 Play on Words

本文介绍了一种使用并查集和欧拉路算法解决单词链问题的方法。通过构建有向图,利用并查集判断连通性,并结合欧拉路原理验证图是否满足特定条件,从而判定单词链是否可以形成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:将n个单词首尾相连组成一个单词链,如:acm->malform->mouse(a -> m -> m -> m ->m -> e)
   并查集+欧拉路实现
      1.并查集判连通,这点不用多说
      2.欧拉路,由图中可知除二端点外,其余字母的入度和出度均相等,二端点的出度和入度相差1,还有一种可能是,整个图就是一个欧拉回路,此时每个端点的入度和出度均相等


//模板开始
#include <string>   
#include <vector>   
#include <algorithm>   
#include <iostream>   
#include <sstream>   
#include <fstream>   
#include <map>   
#include <set>   
#include <cstdio>   
#include <cmath>   
#include <cstdlib>   
#include <ctime>
#include<iomanip>
#include<string.h>
#define SZ(x) (int(x.size()))
using namespace std;

int toInt(string s){
	istringstream sin(s); 
	int t; 
	sin>>t; 
	return t;
}
template<class T> string toString(T x){
	ostringstream sout; 
	sout<<x; 
	return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
	istringstream sin(s); 
	int64 t; 
	sin>>t;
	return t;
}
template<class T> T gcd(T a, T b){ 
	if(a<0) 
		return gcd(-a, b);
	if(b<0) 
		return gcd(a, -b);
	return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)

#define ifs cin


int findset(int x, int pa[])
{
	return pa[x] != x ? pa[x] = findset(pa[x], pa) : x;
}

//【图论05】并查集 1001 Play on Words
#define MAX_SIZE 30
int next_node[MAX_SIZE];		//存储有向图的边
int in[MAX_SIZE];		//存储节点的入度
int out[MAX_SIZE];		//存储节点的出度
int flag[MAX_SIZE];		//标记节点是否存在

void init()		//初始化
{
	for(int i = 0; i < 26; i++)
	{
		next_node[i] = i;
	}
	memset(in, 0, sizeof(in));
	memset(out, 0, sizeof(out));
	memset(flag, 0, sizeof(flag));
}

int findset(int a)		//找元素所在集合的代表元(因为用了路径压缩,路径压缩的主要目的是为了尽快的确定元素所在的集合)
{
	while(next_node[a] != a)
	{
		a = next_node[a];
	}
	return a;
}

void union_nodes(int a, int b)		//集合合并
{
	int a1 = findset(a);
	int b1 = findset(b);
	next_node[a1] = b1;
}

int main()
{
	//ifstream ifs("shuju.txt", ios::in);
	int m, n;
	ifs>>m;
	for(int i = 0; i < m; i++)
	{
		init();
		ifs>>n;
		for(int j = 0; j < n; j++)		//输入数据,建立有向图,并合并相关集合
		{
			char data[1005];
			ifs>>data;
			int a = data[0] - 'a';
			int b = data[strlen(data) - 1] - 'a';
			union_nodes(a, b);
			out[a]++;
			in[b]++;
			flag[a]++;
			flag[b]++;
		}

		int count = 0;
		for(int j = 0; j < 26; j++)		//计算有向图中连通分支的个数
		{
			if(next_node[j] == j && flag[j] != 0)
			{
				count++;
			}
		}

		if(count >= 2)		//当连通分支大于2
		{
			cout<<"The door cannot be opened."<<endl;
			continue;
		}
		
		int f1 = 1;
		if(count == 0)		//当构成回路
		{
			for(int j = 0; j < 26; j++)
			{
				if(flag[j] == 0)
				{
					continue;
				}
				if(in[j] != out[j])
				{
					cout<<"The door cannot be opened."<<endl;
					f1 = 0;
					break;
				}
			}
			if(f1 == 1)
			{
				cout<<"Ordering is possible."<<endl;
				continue;
			}
			else if(f1 == 0)
			{
				continue;
			}
		}

		int jishu1 = 1;
		int jishu2 = 1;
		int f2 = 1;
		if(count == 1)		//当存在一个不是回路的连通分量
		{
			for(int j = 0; j < 26; j++)
			{
				if(in[j] == out[j])
				{
					continue;
				}
				else if(in[j] - out[j] == 1)
				{
					jishu1--;
				}
				else if(out[j] - in[j] == 1)
				{
					jishu2--;
				}
				else
				{
					cout<<"The door cannot be opened."<<endl;
					f2 = 0;
					break;
				}
			}
			if(f2 == 1)
			{
				cout<<"Ordering is possible."<<endl;
				continue;
			}
			else if(f2 == 0)
			{
				continue;
			}
		}
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值