• 欢迎访问废江's博客 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏本站吧

05-树8 File Transfer

浙大mooc 站点默认 2周前 (11-27) 9次浏览 已收录 0个评论

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.

题意:就是考查并查集的知识,姥姥的视频说的实在太好了,我也是看了那个视频用的姥姥的方法。就直接贴我写的代码了

#include<iostream>
#define maxsize 10005
using namespace std;
typedef int elemtype;//并查集的数据的类型
typedef elemtype settype[maxsize];//并查集类型,默认为数组下标存储数据值,数组值表示父节点,集合的简化表示
void initsettype(settype s,int n) {
	for (int i = 0; i < n; i++) {
		s[i] = -1;
	}
}
elemtype find(settype s, elemtype x) {
	for (; s[x] > 0; x = s[x]);
	return x;
}
void unionn(settype s, elemtype x1, elemtype x2) {
	if (s[x1] < s[x2]) {
		s[x1] += s[x2];
		s[x2] = x1;
	}
	else {
		s[x2] += s[x1];
		s[x1] = x2;
	}
}
void input_connection(settype s) {
	int x1, x2;
	cin >> x1 >> x2;
	int root1 = find(s, x1 - 1);
	int root2 = find(s, x2 - 1);
	if (root1 != root2)
		unionn(s, root1, root2);
}
void check_connection(settype s) {
	int x1, x2;
	cin >> x1 >> x2;
	int root1 = find(s, x1 - 1);
	int root2 = find(s, x2 - 1);
	if (root1 == root2)
		cout << "yes" << endl;
	else
		cout << "no" << endl;
}
void check_network(settype s, int n) {
	int counter = 0;
	for (int i = 0; i < n; i++) {
		if (s[i] < 0)
			counter++;
	}
	if (counter == 1)
		cout << "The network is connected.";
	else
		cout << "There are " << counter << " components.";
}
int main() {
	settype s;//声明并查集s
	int n;
	cin >> n;
	initsettype(s, n);//对并查集初始化
	char in;
	do {
		cin >> 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;
}

个人博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:05-树8 File Transfer
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到