前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >05-树8 File Transfer

05-树8 File Transfer

作者头像
废江_小江
发布2022-09-05 11:40:38
1850
发布2022-09-05 11:40:38
举报
文章被收录于专栏:总栏目

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.

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

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-27),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档