POJ 2513 Trie树+并查集

题意:一根木棍的两端都涂有颜色,现在有一系列的木棍,问能否将它们连成一条直线,使两木棍的结合点处颜色一致。
解法:以颜色为节点,木棍为边。 通过字典树给颜色编号,然后通过并查集来检验图的连通性,最后统计每种颜色的度,判断是否是欧拉图或者欧拉半图。

#include <iostream>
using namespace std;

const int kind = 26;
const int MAX = 500005;
int color[MAX], father[MAX];
int node_num = 0, color_id = 0;

struct Tree
{
	int key;
	Tree *next[kind];
} node[1000005], root;

int Insert ( char* word )
{
	Tree *location = &root;
	int i = 0, id;
	while ( word[i] )
	{
		id = word[i] - 'a';
		if ( location->next[id] == NULL )
			location->next[id] = &node[++node_num];
		location = location->next[id];
		++i;
	}
	if ( location->key == 0 )
		location->key = ++color_id;
	return location->key;
}

int find_set( int x )  /*此处改了一个多小时,痛苦!仔细和下一段代码的并查集做比较*/
{
	if ( father[x] >= 0 )
		father[x] = find_set(father[x]);
	if ( father[x] < 0 )
		return x;
	else
		return father[x];
}

void Union ( int x, int y )
{
	int tx=find_set(x);
    int ty=find_set(y);
    if( tx == ty  ) return; /*根节点储存节点的个数,其余子节点储存其父节点*/
	if ( father[tx] < father[ty] )
	{
		father[tx] += father[ty];
		father[ty] = tx;
	}
	else
	{
		father[ty] += father[tx];
		father[tx] = ty;
	}
}

bool check ()
{
	int cnt1 = 0, cnt2 = 0;
	for ( int i = 1; i <= color_id; ++i )
	{
		if ( father[i] < 0 ) ++cnt1; //cnt1 = 1保证只存在一个联通分支
		if ( color[i] % 2 != 0 ) ++cnt2; //cnt2 = 0,2保证存在欧拉路径或者欧拉回路
	}
	if ( (cnt2 != 2 && cnt2 != 0) || cnt1 > 1 )
		return false;
	return true;
}

int main()
{
	int a, b;
	char str1[11], str2[11];
	memset(father,-1,sizeof(father));
	memset(color,0,sizeof(color));
	memset(node,NULL,sizeof(node));
	root = node[0];

	while ( scanf("%s%s",str1,str2) != EOF ) 
	{
		a = Insert(str1); 
		b = Insert(str2); 
		color[a]++;
		color[b]++;
		Union(a,b);	
	
	}
	if ( check() )
		printf("Possible\n");
	else
		printf("Impossible\n");
	return 0;
}


下面是有区别的并查集:
#include <iostream>
using namespace std;

const int kind = 26;
const int MAX = 500005;
int color[MAX], father[MAX], r[MAX];
int node_num = 0, color_id = 0;

struct Tree
{
	int key;
	Tree *next[kind];
	Tree()
	{
		key = 0;
		for ( int i = 0; i < kind; ++i )
			next[i] = NULL;
	}
} node[1000005], root;

int Insert ( char* word )
{
	Tree *location = &root;
	int i = 0, id;
	while ( word[i] )
	{
		id = word[i] - 'a';
		if ( location->next[id] == NULL )
			location->next[id] = &node[++node_num];
		location = location->next[id];
		++i;
	}
	if ( location->key == 0 )
	{
		++color_id;
		location->key = color_id;
		return location->key;
	}
	else return location->key;
}

int find_set( int x )  /*一开始将这个函数直接放在上一篇代码中,结果错的没边了*/
{
	if( father[x] != x )
		father[x] = find_set(father[x]);
	return father[x];
}

void Union ( int x, int y )
{
	int tx = find_set(x);
	int ty = find_set(y);
	if ( tx == ty ) return;
	if ( r[tx] > r[ty] )
		father[ty] = tx;
	else
		father[tx] = ty;
	if ( r[tx] == r[ty] )
		r[ty]++;
}

bool check ()
{
	int cnt = 0;
	int mark = find_set(1);
	for ( int i = 1; i <= color_id; ++i )
	{
		if ( find_set(i) != mark  )
			return false;
		if ( color[i] % 2 != 0 )
			++ cnt;
	}
	if ( cnt != 2 && cnt != 0 )
		return false;
	else return true;
}

int main()
{
	int a, b;
	char str1[11], str2[11];
	for ( int i = 0; i < MAX; ++i )
	{
		father[i] = i;
		color[i] = r[i] = 0;
	}
	while ( scanf("%s%s",str1,str2) != EOF )
	{
		a = Insert(str1);
		b = Insert(str2);
		color[a]++;
		color[b]++;
		Union(a,b);	
	}
	if ( check() )
		printf("Possible\n");
	else
		printf("Impossible\n");
	return 0;
}




你可能感兴趣的:(struct,tree,null,insert)