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:
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.

我的代码:C++(g++ 6.5.0)

#include <iostream>

#define MaxSize 10001
typedef int elementType; /*默认元素可以用非负整数表示*/
typedef int setName; /*默认根节点的下标作为集合名称*/
//typedef elementType setType[MaxSize];
typedef setName * setType;

/*S[0]空间不用,计算机序号与数组下标一一映射*/
setType Initialization(int N) {
	setType S = (setName *)malloc((N+1)*sizeof(elementType));
	for (int i = 1; i <= N; i++) S[i] = -1;
	return S;
}
void Union(setType S,setName root1,setName root2) {
	if (S[root1] < S[root2] ) {
		S[root1] += S[root2];
		S[root2] = root1;
	}else {
		S[root2] += S[root1];
		S[root1] = root2;
	}
}
setName Find(setType S, elementType X) {
	if (S[X] < 0) return X;
	else return S[X] = Find(S,S[X]); //路径压缩	
}
void Input_connection(setType S) {
	elementType c1, c2;
	scanf("%d %d\n",&c1,&c2);
	setName root1 = Find(S, c1);
	setName root2 = Find(S, c2);
	if (root1 != root2) Union(S, root1, root2);
}
void Check_connection(setType S) {
	elementType c1, c2;
	scanf("%d %d\n", &c1, &c2);
	setName root1 = Find(S, c1);
	setName root2 = Find(S, c2);
	if (root1 == root2) printf("yes\n");
	else printf("no\n");
}
void Check_network(setType S,int N) {
	int cnt = 0;
	for (int i = 1; i <= N; i++) {
		if (S[i] < 0) ++cnt;
	}
	if (cnt > 1) printf("There are %d components.\n",cnt);
	else printf("The network is connected.\n");
}

int main()
{
	//setType S;
	int N;
	char in;
	scanf("%d",&N);
	//Initialization(S,N);
	setType S = Initialization(N);
	rewind(stdin); // 键盘文件当前位置指针指向文件头(起到清空键盘缓冲区中无用字符的作用)
	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;
}
posted @ 2020-09-06 23:26  敵人杰  阅读(214)  评论(0编辑  收藏  举报