#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 10
#define NULL_DATA -1
class Node
{
public:
Node(int x=SIZE)
{
data = new int[x];
for(int i=0;i<x;i++)
{
data[i] = NULL_DATA;
}
}
void Union(int root1,int root2)
{
int x=Find(root1);
int y=Find(root2);
if(x!=y)
{
if(data[x]<data[y])
{
data[x]+=data[y];
data[y]=x;
}
else
{
data[y]+=data[x];
data[x]=y;
}
}
}
int Find(int x)
{
while(data[x]>0)
x=data[x];
return x;
}
void view()
{
for(int i=0;i<SIZE;i++)
{
cout<<setw(4)<<data[i];
}
cout<<endl;
for(int j=0;j<SIZE;j++)
{
cout<<setw(4)<<j;
}
cout<<endl;
}
private:
int *data;
};
int main()
{
Node node;
node.Union(1,2);
node.Union(2,3);
node.Union(4,3);
node.Union(4,5);
node.Union(6,7);
node.Union(7,9);
node.Union(9,10);
node.view();
return 0;
}
c++集合。
最新推荐文章于 2025-03-25 18:55:42 发布