P - 单词 (p169, 欧拉回路)

42 篇文章 0 订阅

题意:我一看欧拉回路,正好我有个板子就a了这道题了

思路:

欧拉回路就是能用一条路把所有点都连接的路没有分支,详见七桥问题

欧拉回路无非就是判断每点出度入度的是否相同,不同看看是不是起点和终点的问题即(找一个入度差一和出度也差一也行)就行,并且要用并查集判断一下是不是在一个联通快上。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 100009;
int f[10009];
int id[1009];
int cd[1009];
int find(int x) {
	if(f[x] == x) {
		return x;
	}
	else {
		return f[x] = find(f[x]);
	}
}
int union1(int x, int y) {
	x = find(x);
	y = find(y);
	if(x == y) {
		return 1;
	}
	else {
		f[x] = y;
		return 0;
	}
}
int main() {
	int n;
	scanf("%d", &n);
	while(n--) {
		memset(cd, 0, sizeof(cd));
		memset(id, 0, sizeof(id));
		int h;
		scanf("%d", &h);
		for(int i = 0;i <= 26;++i) {
			f[i] = i;
		}
		for(int i = 1;i <= h;++i) {
			string s;
			cin >> s;
			int x = s[0]-'a';
			int y = s[s.length()-1]-'a';
			union1(x, y);
			id[y]++;
			cd[x]++;
		}
		int sum = 0;
		for(int i = 0;i < 26;++i) {
			if( (id[i] || cd[i]) && find(i) == i) {
				sum++;
			}
		}
		if(sum != 1) {
			printf("The door cannot be opened.\n");continue;
		}
		int cnt = 0, cnt1 = 0 ,cnt2 = 0, flagh = 0;
		for(int i = 0;i < 26;++i) {
			if(id[i] == cd[i]) {
				cnt++;
			}
			else if(id[i]-cd[i] == 1){
				cnt1++;
			}
			else if(cd[i]-id[i] == 1) {
				cnt2++;
			}
			else {
				flagh++;
			}
		}//查找头部按字典序
		if((cnt1 == cnt2 && cnt1 != 1 && cnt1 != 0) || cnt1 != cnt2) {
			printf("The door cannot be opened.\n");continue;
		}
		if(flagh) {
			printf("The door cannot be opened.\n");continue;
		}	
		printf("Ordering is possible.\n");
	}
	return 0;
}
/*
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值