05-树8 File Transfer(C)

日常,满分

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≤104), 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.

 起初我是老实运用, 满分

struct UnionFind{
	int data;
	int Parent;
};
#include
#include

typedef struct UnionFind *UF;
struct UnionFind{
	int data;
	int Parent;
};

void Build_UnionFindTree(UF T, int num);
UF Init_UnionFindTree(int num);
int Find_Value(UF T,int data);
void Check_Tree(UF T, int n1, int n2);
void Union_Tree(UF T, int n1, int n2);
int Component_num(UF T, int num);

int main()
{
	UF T;
	int num, count;
	scanf("%d", &num);
	T = Init_UnionFindTree(num);
	Build_UnionFindTree(T, num);
	count = Component_num(T, num);
	if(count == 1){
		printf("The network is connected.\n");
	}else{
		printf("There are %d components.\n", count);
	}
	return 0;
}
void Build_UnionFindTree(UF T, int num)
{
	char ch;
	int n1, n2;
	while(scanf(" %c", &ch), ch != 'S'){
		switch(ch){
			case 'C':
				scanf("%d%d", &n1, &n2);
				Check_Tree(T, n1, n2);
				break;
			case 'I':
				scanf("%d%d", &n1, &n2);
				Union_Tree(T, n1, n2);
				break;
			default:
				printf("出现不合规定字符!\n");
				while((ch = getchar()) != '\n' && ch != EOF);
				break;
		}
	}
}
UF Init_UnionFindTree(int num)
{
	UF T;
	T = (UF)malloc(sizeof(struct UnionFind) * num);
	for(int i = 0; i < num; i++){
		T[i].data = i + 1;
		T[i].Parent = -1;
	}
	return T;
}
int Find_Value(UF T, int data)
{
	if(T[data].Parent < 0){
		return data;
	}
	return Find_Value(T, T[data].Parent);
}
void Check_Tree(UF T, int n1, int n2)
{
	if(Find_Value(T, n1 - 1) != Find_Value(T, n2 - 1)){
		printf("no\n");
	}else{
		printf("yes\n");
	}
}
void Union_Tree(UF T, int n1, int n2)
{
	int r1, r2;
	r1 = Find_Value(T, n1 - 1);
	r2 = Find_Value(T, n2 - 1);
	if(T[r1].Parent < T[r2].Parent){
		T[r1].Parent += T[r2].Parent;
		T[r2].Parent = r1;
	}else{
		T[r2].Parent += T[r1].Parent;
		T[r1].Parent = r2;
	}	
}
int Component_num(UF T, int num)
{
	int count = 0;
	for(int i = 0; i< num; i++){
		if(T[i].Parent < 0){
			count++;
		}
	}
	return count;
}

接下来便运用数组了解题,这个也是满分

其实为什么写数组,是因为上面代码最开始是超时的,早上醒来,才想起来这是结构体数组,所以才有下述改动:

//所以由
int main()
{
	UF T;
	int num, count;
	scanf("%d", &num);
	T = Build_UnionFindTree(num);
	count = Component_num(T, num);
	if(count == 1){
		printf("The network is connected.\n");
	}else{
		printf("There are %d components.\n", count);
	}
	return 0;
}
//改为下面的形式,没想到,小小的一变化,本来Init_UnionFindTree(num)是在
//Build_UnionFindTree(T, num);函数内部的
int main()
{
	UF T;
	int num, count;
	scanf("%d", &num);
	T = Init_UnionFindTree(num);
	Build_UnionFindTree(T, num);
	count = Component_num(T, num);
	if(count == 1){
		printf("The network is connected.\n");
	}else{
		printf("There are %d components.\n", count);
	}
	return 0;
}
//最初的/Build_UnionFindTree(T, num);
UF Build_UnionFindTree(int num)
{
	UF T;
	char ch;
	int n1, n2;
	T = Init_UnionFindTree(num);
	while(scanf(" %c", &ch), ch != 'S'){
		switch(ch){
			case 'C':
				scanf("%d%d", &n1, &n2);
				Check_Tree(T, num, n1, n2);
				break;
			case 'I':
				scanf("%d%d", &n1, &n2);
				Union_Tree(T, num, n1, n2);
				break;
			default:
				printf("出现不合规定字符!\n");
				while((ch = getchar()) != '\n' && ch != EOF);
				break;
		}
	}
	return T;
}

真的难以想象,小小的变动,便有如此大的区分。
具体的原因,我也不知道。希望观看者解惑

#include
#include

int Find(int *BF, int data);
void Union(int *BF, int n1, int n2);
void Check(int *BF, int n1, int n2);
int Component_num(int *BF, int num);

int main()
{
	int *BF;
	int num, count, n1, n2;
	char ch;
	scanf("%d", &num);
	BF = (int*)malloc(sizeof(int) * (num+1));
	for(int i = 0; i <= num; i++){
		BF[i] = -1;
	}
	while(scanf(" %c", &ch), ch != 'S'){
		switch(ch){
			case 'C':
				scanf("%d%d", &n1, &n2);
				Check(BF, n1, n2);
				break;
			case 'I':
				scanf("%d%d", &n1, &n2);
				Union(BF, n1, n2);
				break;
			default:
				printf("出现不合规定字符!\n");
				while((ch = getchar()) != '\n' && ch != EOF);
				break;
		}
	}
	count = Component_num(BF, num);
	if(count == 1){
		printf("The network is connected.\n");
	}else{
		printf("There are %d components.\n", count);
	}
	return 0;
}
int Find(int *BF, int data)
{
	if(BF[data] < 0){
		return data;
	}
	return Find(BF, BF[data]);
} 
void Union(int *BF, int n1, int n2)
{
	int r1 = Find(BF, n1);
	int r2 = Find(BF, n2);
	if(BF[r1] < BF[r2]){
		BF[r1] += BF[r2];
		BF[r2] = r1;	
	}else{
		BF[r2] += BF[r1]; 
		BF[r1] = r2; 
	}
	return ;
}
void Check(int *BF, int n1, int n2)
{
	if(Find(BF, n1) != Find(BF, n2)){
		printf("no\n");
	}else{
		printf("yes\n");
	}
}
int Component_num(int *BF, int num)
{
	int count = 0;
	for(int i = 1; i<= num; i++){
		if(BF[i] < 0){
			count++;
		}
	}
	return count;
}

你可能感兴趣的:(c语言,数据结构)