例如:
<小明,小王>,<小军,小王>,<小丽,小李>是队友,那么小军和小明是队友,小军和小丽不是队友。
#include<iostream>
#define MAXNUM 100
using namespace std;
static char* name[MAXNUM];
static int group[MAXNUM];
static int rank[MAXNUM];
void make_set()
{
for(int i=0;i<MAXNUM;i++)
{
group[i]=i;
rank[i]=0;
}
}
int findGroup(int i)
{
if(group[i]==i)
{
return group[i];
}
group[i]=findGroup(group[i]);
}
int myfindGroup(int i)
{
return group[i];
}
void union1(int i,int j)
{
int g1=group[i];
int g2=group[j];
//intg1=findGroup(group[i]);
//intg2=findGroup(group[j]);
//cout<<"g1="<<g1<<""<<"g2="<<g2<<endl;
if(rank[g1]>rank[g2])
{
group[j]=g1;
}
else
{
group[i]=g2;
if(rank[g1]==rank[g2])
{
rank[g2]++;
}
}
}
bool judgeGroup(char* name1,char* name2)
{
intindex1=0,index2=0;
for(int i=0;i<MAXNUM;i++)
{
if(!strcmp(name1,name[i]))
{
index1=i;
break;
}
}
for(int j=0;j<MAXNUM;j++)
{
if(!strcmp(name2,name[j]))
{
index2=j;
break;
}
}
if(group[index1]==group[index2])
return true;
else
return false;
}
int main()
{
char * name1;
char * name2;
name[0]="小明";
name[1]="小王";
name[2]="小军";
name[3]="小丽";
name[4]="小李";
make_set();
union1(0,1);
union1(2,1);
union1(3,4);
for(int i=0;i<5;i++)
{
cout<<group[i]<<endl;
}
name1=(char*)malloc(sizeof(char)*10);
name2=(char*)malloc(sizeof(char)*10);
cin>>name1;
cin>>name2;
if(judgeGroup(name1,name2))
{
cout<<"是队友."<<endl;
}
else
{
cout<<"不是队友"<<endl;
}
system("pause");
return 0;
}