C 树表示集合 05-树8 File Transfer (25分) 按秩归并+路径压缩

05-树8 File Transfer (25分)

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?

Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤10e+4 ), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:

I c1 c2  

where I stands for inputting a connection between c1 and c2; or

C c1 c2    

where C stands for checking if it is possible to transfer files between c1 and c2; or

S

where S stands for stopping this case.

Output Specification:
For each C case, print in one line the word “yes” or “no” if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line “The network is connected.” if there is a path between any pair of computers; or “There are k components.” where k is the number of connected components in this network.

Sample Input 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S

Sample Output 1:
no
no
yes
There are 2 components.

Sample Input 2:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S

Sample Output 2:

no
no
yes
yes
The network is connected.

main函数控制输入输出
立了个flag判断是否需要结束输入;

int main()
{
	cin>>N;
	for(int i=1;i<=N;i++)
	{
		result[i]=-1;
	}
	int flag=0;
	while(cin>>cmd){
	
	if(cmd!='S')
	cin>>a>>b;
	
	switch(cmd){
		case 'C':
			Check(a,b);
			break;
		case 'I':
			Input(a,b);
			break;
		case 'S':
			Stop();
			flag=1;
			break;
	}
	if(flag)
	 	break;
}
}

来一波对比,看一下和老师答案main函数的区别

int main()
{
	SetType S;
	int n;
	char in;
	scanf("%d\n",&n);
	Initialization(S,n);   //初始化S——为-1 
	do{
		scanf("%c",&in);
		switch(in)
		{
			case 'I':Input_connection(S);break;
			case 'C':Check_connection(S);break;
			case 'S':Check_network(S,n);break;
		}
	}
	while(in != 'S');
	return 0;
}

用do while,将终止条件放进while中,可以省去标志符号flag;


input 函数,进行了按秩归并
将小的集合放入大的集合内,以减缓树的高度

void Input(int a,int b){
	
	while(result[a]>0) a=result[a];  //指向a所在的集合名
	while(result[b]>0) b=result[b];

	if(result[a]<result[b]){     //集合a更大 
		result[a]+=result[b];    //集合b挂在a上,a的数量继续增加 
		result[b]=a; 
	}
	else {
		result[b]+=result[a];  //集合a挂在集合b上 
		result[a]=b;
	}
}

check函数,判断两个节点是否属于同一集合

void Check(int a, int b) 
{
	while(result[a]>0) a=result[a];  //指向a所在的集合名 
	while(result[b]>0) b=result[b];
	if(a==b) cout<<"yes"<<endl;
	else cout<<"no"<<endl;
 } 

Stop()函数,完成对集合数的统计,并输出
剩余多少个-1,即有多少个集合

void Stop(){
	int count=0;
	for(int i=1;i<=N;i++)                 //有几个-1,就有几个集合! 脑淤血这都没想到 
	{
		if(result[i]<0)
		++count;
	}

	if(count>1)	cout<<"There are "<<count<<" components."<<endl;
	else	cout<<"The network is connected."<<endl;
}

完整代码

#include
#include
#include
using namespace std;

//check 判断a和b是否属于同一集合
//input 将a和b放入同一集合 

#define MAXNUM 10010
int result[MAXNUM];      //初始全部为-1,即都为根节点 

void Input(int a,int b){
	
	while(result[a]>0) a=result[a];  //完成find的工作 
	while(result[b]>0) b=result[b];
	
	if(result[a]<result[b]){     //集合a更大 
		result[a]+=result[b];    //集合b挂在a上,a的数量继续增加 
		result[b]=a; 
	}
	else {
		result[b]+=result[a];  //集合a挂在集合b上 
		result[a]=b;
	}
}


void Check(int a, int b) 
{
	while(result[a]>0) a=result[a];  //完成find的工作 
	while(result[b]>0) b=result[b];
	if(a==b) cout<<"yes"<<endl;
	else cout<<"no"<<endl;
 } 
 
 
int N;
char cmd;
int a,b;

void Stop(){
	int count=0;
	for(int i=1;i<=N;i++)                 //有几个-1,就有几个集合! 脑淤血这都没想到 
	{
		if(result[i]<0)
		++count;
	}

	if(count>1)	cout<<"There are "<<count<<" components."<<endl;
	else	cout<<"The network is connected."<<endl;

}

int main()
{
	cin>>N;
	for(int i=1;i<=N;i++)
	{
		result[i]=-1;
	}
	int flag=0;
	while(cin>>cmd){
	
	if(cmd!='S')
	cin>>a>>b;
	
	switch(cmd){
		case 'C':
			Check(a,b);
			break;
		case 'I':
			Input(a,b);
			break;
		case 'S':
			Stop();
			flag=1;
			break;
	}
	if(flag)
	 	break;
}
}

注意点
没有单独设置find函数,故没法递归实现路径压缩。
每次input和check都需要将两个数循环得到所属集合名,再进行操作。

参考代码

#include
using namespace std;
#define MaxSize 10010

typedef int ElementType;      //默认元素用非负整数表示 
typedef int SetName;         //根节点下标作为集合名称 
typedef ElementType SetType[MaxSize]; 

//路径压缩
SetName Find(SetType S, ElementType X)   //返回集合名称 
{
	if(S[X]<0)   //找到集合的根
		return X;
	else 
		return S[X]=Find(S,S[X]);
		//找到根,把根变成X的父节点,再返回根 
}


void Input_connection(SetType S); 
void Check_connection(SetType S); 
void Check_network(SetType S, int n);

void Union(SetType S, SetName Root1,SetName Root2)
{
	if(S[Root2]<S[Root1]){     //集合2更大 
		S[Root2]+=S[Root1];    //集合1挂在2上,2的数量继续增加 
		S[Root1]=Root2; 
	}
	else {
		S[Root1]+=S[Root2];  //集合2挂在集合1上 
		S[Root2]=Root1;
	}
}

void Initialization(SetType S, int n)
{
	for (int i=0;i<n;i++)
		S[i]=-1;
 } 

int main()
{
	SetType S;
	int n;
	char in;
	scanf("%d\n",&n);
	Initialization(S,n);   //初始化S——为-1 
	do{
		scanf("%c",&in);
		switch(in)
		{
			case 'I':Input_connection(S);break;
			case 'C':Check_connection(S);break;
			case 'S':Check_network(S,n);break;
		}
	}
	while(in != 'S');
	return 0;
}

void Input_connection(SetType S)
{
	ElementType u,v;
	SetName Root1,Root2;        
	scanf("%d %d\n",&u,&v);      
	Root1=Find(S,u-1);       //找到这个数所属的集合 
	Root2=Find(S,v-1);        
	if(Root1 !=Root2)       //root都为最上层集合名称 
		Union(S,Root1,Root2);   //并起来 
}

void Check_connection(SetType S)
{
	ElementType u,v;
	SetName Root1,Root2;
	scanf("%d %d\n",&u,&v);
	Root1=Find(S,u-1);
	Root2=Find(S,v-1);
	if(Root1==Root2)
		printf("yes\n");
	else printf("no\n");
}

void Check_network(SetType S, int n)
{
	int i,counter=0;
	for(i=0;i<n;i++)
	{
		if(S[i]<0) counter++;
	}
	if(counter==1)
		printf("The network is connected.\n");
	else
		printf("There are %d components.\n",counter);
}

路径压缩在find函数内实现
1.路径压缩的递归方法
找到根,把根变成X的父节点,再返回根

SetName Find(SetType S, ElementType X)   //返回集合名称 
{
	if(S[X]<0)   //找到集合的根
		return X;
	else 
		return S[X]=Find(S,S[X]);
		//找到根,把根变成X的父节点,再返回根 
}

2.路径压缩的循环方法
这个压缩只是实现了从跳过一个父节点的压缩,并不像递归方法可以压缩至根节点;

int find(int *parent,int p){
    while(p >0){
    if(parent[parent[p]]>0)
	parent[p]=parent[parent[p]];
        p=parent[p];
    }
    return p;
}

按秩排序在union函数内实现
1.高度小的集合放入高度高的集合中,高度归并

void Union(SetType S, SetName Root1,SetName Root2)
{
	if(S[Root2]<S[Root1])      //树2高度大 
		S[Root1]=Root2;        // 树1指向树2
	else{
		if(S[Root1]==S[Root2]) S[Root1]--; //高度相等,树1高度+1 
		S[Root2]=Root1;     //树2指向树1,挂在树1上 
	} 
 } 

2.成员少的集合放入成员多的集合中,规模归并

void Union(SetType S, SetName Root1,SetName Root2)
{
	if(S[Root2]<S[Root1]){     //集合2更大 
		S[Root2]+=S[Root1];    //集合1挂在2上,2的数量继续增加 
		S[Root1]=Root2; 
	}
	else {
		S[Root1]+=S[Root2];  //集合2挂在集合1上 
		S[Root2]=Root1;
	}
}

路径压缩会改变数的高度,而不会改变树的规模,所以路径压缩配合规模归并更方便;

总结
集合的操作都需要用到
find——找到该元素属于哪个集合;
union——将两个集合合并;
故创建这两个常用函数可以方便很多;

你可能感兴趣的:(数据结构,树)