【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班被碾压记正式开启!!    

猜你喜欢

转载自blog.csdn.net/bill_benation/article/details/80917965