05-树8 File Transfer(文件传输)--路径压缩

题目描述
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≤10^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:
每个输入文件包含一个测试用例。对于每个测试用例,第一行包含N(2 ≤ Ñ ≤ 1 0^4 ),一个网络中的计算机的总数。然后,网络中的每台计算机都由1到1之间的正整数表示ñ。然后在以下行中,输入以下列格式给出

I c1 c2
where I stands for inputting a connection between c1 and c2; or
其中I表示输入c1和之间的连接c2; 要么

C c1 c2
where C stands for checking if it is possible to transfer files between c1 and c2; or
where C代表检查是否可以在c1和之间传输文件c2; 要么

S
where S stands for stopping this case.
在哪里S代表停止这种情况。


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.
对于每种C情况,如果有可能或不可能分别在c1和之间传输文件,则在一行中打印“是”或“否” c2。在每种情况结束时,在一行中打印“网络已连接”。如果任何一台计算机之间有路径; 或“有k组件。” k此网络中连接组件的数量在哪里。


Sample Input 1:
样本输入1:

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

Sample Output 1:
样本输出1:

no
no
yes
There are 2 components.


Sample Input 2:
样本输入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:
样本输出2:

no
no
yes
yes
The network is connected.



思路解答:基础操作


代码详解:

#include
#include
#define Maxsize 10000
int Arry[Maxsize]; 
int FindRoot(int num);
void Connect();
void Check();
void Compute(int N);
int main()
{
	int N,i = 0;
	char ch;
	scanf("%d",&N);//计算机的个数
	for(i = 1; i <= N ;i++)//初始化 
		Arry[i] = -1;
	scanf("%c",&ch);//操作表示
	while( ch != 'S')
	{
		switch( ch )
		{
			case 'C':Check();break;//检查
			case 'I':Connect();break;//连接
			default:break;
		}
		scanf("%c",&ch);
	} 
	if(ch == 'S')
		Compute(N);//计算连接部分数
	return 0;
}
int FindRoot(int num)//找根,路径压缩:先找到根; 把根变成 X 的父结点; 再返回根。
{
	if( Arry[num] < 0)//为负数时,为根
		return num;
	else
		return Arry[num] = FindRoot(Arry[num]);//尾递归
}
void Connect()
{
	int num1,num2,root1,root2;
	scanf("%d %d",&num1,&num2);
	root1 = FindRoot(num1);//找根
	root2 = FindRoot(num2);
	if(root1 != root2)//如果不同根
	{
		if(Arry[root1] < Arry[root2])//比较两个根的数的孩子的数量
		{
			Arry[root1] += Arry[root2]; 
			Arry[root2] =  root1; 
		}
		else
		{
			Arry[root2] += Arry[root1]; 
			Arry[root1] =  root2; 
		}
	}
	return ;
}
void Check()
{
	int num1,num2,root1,root2;
	scanf("%d %d",&num1,&num2);
	root1 = FindRoot(num1);
	root2 = FindRoot(num2);
	if(root1 != root2)
		printf("no\n");
	else
		printf("yes\n");
	return ; 
}
void Compute(int N)
{
	int counter = 0,i = 0;
	for(i = 1; i <= N ;i++)
		if(Arry[i] < 0) counter++;
	if(counter == 1)
		printf("The network is connected.\n");
	else
		printf("There are %d components.",counter);
	return ; 
}

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