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;
};
4最大N,递增链,卡不按大小union的376150

运行超时

0 / 4
5最大N,递减链,卡不按大小union的376150

运行超时

0 / 4
6最大N,两两合并,反复查最深结点,卡不压缩路径的364150

运行超时

0 / 1
#include<stdio.h>
#include<stdlib.h>

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

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

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;
}
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;
}
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 num, int data)
{
	int i;
	for(i = 0; i < num && T[i].data != data; i++){
		if(i >= num){
			return -1;
		}
	}
	for(; T[i].Parent >= 0; i = T[i].data);
	return i;
}
void Check_Tree(UF T, int num, int n1, int n2)
{
	if(Find_Value(T, num, n1) != Find_Value(T, num, n2)){
		printf("no\n");
	}else{
		printf("yes\n");
	}
}
void Union_Tree(UF T, int num, int n1, int n2)
{
	int r1, r2;
	r1 = Find_Value(T, num, n1);
	r2 = Find_Value(T, num, n2);
	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 Find(int *BF, int data)
{
	if(BF[data] < 0){
		return data;
	}
	return Find(BF, BF[data]);
} 

int Find_Value(UF T, int num, int data)
{
	int i;
	for(i = 0; i < num && T[i].data != data; i++){
		if(i >= num){
			return -1;
		}
	}
	for(; T[i].Parent >= 0; i = T[i].data);
	return i;
}

奇怪的是,我的上一个改变Find函数,便会出现问题,不该就超时

#include<stdio.h>
#include<stdlib.h>

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;
}

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值