并查集

关于并查集,参见http://www.cnblogs.com/cyjb/p/UnionFindSets.html,作者:CYBJ,写的很好,要点都在里面


并查集代码如下:


#include <iostream>
#include <string>
using namespace std;


string *a;//存储字符串 不重复
int *represent;//该字符串所属类别
int *rk;//每个结点的高度,没有孩子结点时高度为0
int count = 0;//a中的字符串个数


int findIndex( string s ){
for( int i=0; i<count; i++ ){
if( a[i]==s )
return i;
}
return -1;
}


int find( int index ){
if( represent[index]!=index )
represent[index] = find( represent[index] );
return represent[index];
}


bool isInSameUnion( string s1, string s2 ){
int index1 = findIndex( s1);
int index2 = findIndex( s2);
if( index1==-1 || index2==-1 )
return false;
if( find(index1)==find(index2) )
return true;
else
return false;
}


void printResult( bool result ){
if( result )
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}


int insertString( string s1 ){
int index = count++;
a[index] = s1;
represent[index] = index;
rk[index] = 0;
return index;
}


void unionSet( int index1, int index2 ){
int rep1 = find(index1);
int rep2 = find(index2);
if( rep1==rep2 )
return;
if( rk[rep1]>rk[rep1] )
represent[rep2] = rep1;
else{
represent[rep1] = rep2;
if( rk[rep1]==rk[rep2] )
rk[rep2]++;
}
}


void insertUnion( string s1, string s2 ){
int index1=findIndex(s1);
if( index1==-1 )
index1 = insertString(s1);
int index2=findIndex(s2);
if( index2==-1 )
index2 = insertString(s2);
unionSet(index1,index2);
}


int main(){
int N;
cin>>N;
a= new string[N*2];
represent = new int[N*2];
rk = new int[N*2];

bool indicate=0;
string s1,s2;
for(int i=0; i<N; i++ ){
cin>>indicate;
if( !indicate){//输入并查集
cin>>s1>>s2;
insertUnion(s1,s2);
}


else{//查询并查集
cin>>s1>>s2;
printResult( isInSameUnion(s1,s2) );
}

}

delete[] a;
delete[] represent;
delete[] rk;

return 0;
}



但是在做OJ题目时,发现运行速度上的缺陷。有两个地方耗时:

(1)查找字符串算法:我用了不动脑子的算法,就是挨个比较,可想而知时间复杂度为O(N*L)。看了一位大神写的代码,好像是用状态空间记录,用空间换时间,在O(L)的时间内就可以查找到。

(2)动态生成数组,尤其是多维数组。直接开辟一块较大内存比较快。

下面是优化过的方法。



#include <string.h>
#include <cstring>
#include <stdio.h>
using namespace std;


const int stringSize = 70;//名字最大长度
const int statusNum = 100000*stringSize;//状态空间个数
int status[statusNum][52] ;//状态空间 
int statusRecord[statusNum];//状态空间记录
int represent[200000];//该字符串所属类别
int rk[200000];//每个结点的高度,没有孩子结点时高度为0
int count = 0;//字符串个数
int c=0;//当前状态个数


int add(){
statusRecord[++c]=0;
memset(status[c],0,sizeof(int)*52);
return c;
}


int findIndex( char* s ){
int row=0,column;
char* ptr = s;
while( *ptr != '\0'){
column = ( *ptr>'Z') ? *ptr-'a'+26 : *ptr-'A';
if( status[row][column]==0 ){
status[row][column] = add();
}
row = status[row][column];
ptr++;
}
return statusRecord[row] ? statusRecord[row] : statusRecord[row]=++count;
}


int find( int index ){
if( represent[index]!=index )
represent[index] = find( represent[index] );
return represent[index];
}


bool isInSameUnion( char* s1, char* s2 ){
int index1 = findIndex( s1);
int index2 = findIndex( s2);
if( represent[index1]==0 || represent[index2]==0  )
return false;
if( find(index1)==find(index2) )
return true;
else
return false;
}


void printResult( bool result ){
if( result )
printf("yes\n");
else
printf("no\n");
}


void unionSet( int index1, int index2 ){
int rep1 = find(index1);
int rep2 = find(index2);
if( rep1==rep2 )
return;
if( rk[rep1]>rk[rep2] )
represent[rep2] = rep1;
else{
represent[rep1] = rep2;
if( rk[rep1]==rk[rep2] )
rk[rep2]++;
}
}


void insertUnion( char* s1, char* s2 ){
int index1=findIndex(s1);
int index2=findIndex(s2);


if( represent[index1] == 0 ){
represent[index1] = index1;
rk[index1] = 0;
}
if( represent[index2] == 0 ){
represent[index2] = index2;
rk[index2] = 0;
}
unionSet(index1,index2);
}


int main(){
int N=0;
scanf("%d",&N); 
memset(status[0],0,sizeof(int)*52);
memset(represent,0,sizeof(int)*N*2);
memset(rk,0,sizeof(int)*N*2);


int query=0;
char s1[stringSize],s2[stringSize];
for(int i=0; i<N; i++ ){
scanf("%d%s%s",&query,s1,s2);
if( query==0 )//输入并查集
insertUnion(s1,s2);
else//查询并查集
printResult( isInSameUnion(s1,s2) );
}


return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值