POJ-Colored Sticks(字典树+并查集+欧拉回路)

A - Colored Sticks
Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

这道题是比较经典的题目了。
就是给n根木棒,木棒两端都有颜色,判断能不能把木棒合并成一条,接触的木棒端必须是相同颜色的。

给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。

貌似就是判断欧拉回路。

由图论知识可以知道,无向图存在欧拉路的充要条件为:

①     图是连通的;

②     所有节点的度为偶数,或者有且只有两个度为奇数的节点。

图的连通可以利用并查集去判断。

度数的统计比较容易。

数据比较大,首先需要将颜色的字符串和数字一一对应起来。

虽然可以用map,但是map效率不高,肯定会超时的。

最好的办法的是建立字典树。

将字符串和数字一一对应起来。

注意本题的数据有没有木棒的情况,要输出Possible。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define maxn 500010
#define maxs 26
int parent[maxn];
int degree[maxn];//度数
int color;//颜色编号,从0开始,最后就是颜色总数 
int find (int x)
{
	if(parent[x]==-1)
	return x;
	return find(parent[x]);
}
void  bing(int a,int b)
{
	int t1=find(a);
	int t2=find(b);
	if(t1!=t2)
	  parent[t1]=t2;
}
typedef struct   Trie_Node
{
	bool isword;
    struct Trie_Node  *next[maxs];
	int id;
}Trie;
int insert(Trie *root,char *word)
{
	Trie *p=root;
	int i=0;
	while(word[i]!='\0')
	{
		if(p->next[word[i]-'a']==NULL)
		 {
		 	Trie *temp=new Trie;
		 	temp->isword=0;
		 	for(int j=0;j<maxs;j++)
		 	temp->next[j]=NULL;
		 	temp->isword=0;
		 	temp->id=0;
		    p->next[word[i]-'a']=temp;
		 }
		 p=p->next[word[i]-'a'];
		 i++;
	}
	if(p->isword)
	return p->id;
	else
	{
		p->isword=1;
		p->id=color++;
		return p->id; 
	}  
 } 
 void del(Trie *root)
 {
 	for(int i=0;i<maxs;i++)
 	  if(root->next[i]!=NULL)
 	    del(root->next[i]);  
 	    free(root);
 }
int  main()
{
	char str1[20],str2[20];
	Trie *root=new Trie;
	for(int i=0;i<maxs;i++)
	 root->next[i]=NULL;
	 root->isword=0;
	 root->id=0;
	 color=0;
	 memset(parent,-1,sizeof(parent));
	 memset(degree,0,sizeof(degree)); 
	 while(scanf("%s%s",&str1,&str2)!=EOF)
	 {
	 	int t1=insert(root,str1);
	 	int t2=insert(root,str2);
	 	degree[t1]++;
	 	degree[t2]++;
	 	bing(t1,t2);
	 }
	 int cnt1=0,cnt2=0;//根据parent数组等于-1来找根结点 
	 for(int i=0;i<color;i++)
	 {
	 	if(parent[i]==-1)cnt1++;
	 	if(degree[i]%2==1)cnt2++;
	 	if(cnt1>1)break;
	 	if(cnt2>2)break;
	 }
	 if((cnt1==0 || cnt1==1) && (cnt2==0 || cnt2==2))//存在木棒根数为0的情况 
	 printf("Possible\n");
	 else
	 printf("Impossible\n");
	 del(root);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值