【POJ1386】Play on Words

    先附题

点击打开链接

算法:

    单词建图+欧拉路径判断

AC程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int degree[30];
int father[30];int ran[30];
bool hashs[30];
void inite(){
	for(int i=1;i<=26;i++){degree[i]=0;father[i]=i;ran[i]=1;hashs[i]=0;}
	return;
}
int root_seeking(int x){
	if(father[x]==x)return x;
	father[x]=root_seeking(father[x]);
	return father[x];
}
void unite(int x,int y){
	int a=root_seeking(x);int b=root_seeking(y);
	if(a==b)return;
	if(ran[a]>ran[b]){father[b]=a;ran[a]+=ran[b];}
	else{father[a]=b;ran[b]+=ran[a];}
	return;
}
int main(){
	char st[1010];
	int T;scanf("%d",&T);
	for(int i=1;i<=T;i++){
		inite();
		int n;scanf("%d",&n);
		for(int j=1;j<=n;j++){
			scanf("%s",st);int len=strlen(st);
			int nop1=st[0]-'a'+1;int nop2=st[len-1]-'a'+1;
			degree[nop1]--;degree[nop2]++;
			hashs[nop1]=1;hashs[nop2]=1;
			unite(nop1,nop2);
		}
		int temp_number=0;
		for(int j=1;j<=26;j++){if(hashs[j]&&father[j]==j)temp_number++;}
		if(temp_number>1){puts("The door cannot be opened.");continue;}
		int temp1,temp2,temp3;temp1=temp2=temp3=0;
		for(int j=1;j<=26;j++){
			if(!degree[j])temp1++;
			if(degree[j]==1)temp2++;
			if(degree[j]==-1)temp3++;
		}
		if(temp1==26||(temp1==24&&temp2==1&&temp3==1))puts("Ordering is possible.");
		else puts("The door cannot be opened.");
	}
	return 0;
}

!!!彩蛋time!!!:

    1:水水—yao在夏令营学的第一个算法与发的第一篇博客

2:JSOI夏令营提高1班被碾压记正式开启!!    

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值