Data Structures and Algorithms7-8——File Transfer

我的Data Structures and Algorithms代码仓:https://github.com/617076674/Data-Structures-and-Algorithms

原题链接:https://pintia.cn/problem-sets/16/problems/670

题目描述:

知识点:并查集

思路:并查集

由于需要新增边,且需要一边新增边,一边查询,所以深度优先遍历的做法没有并查集合适。

C++代码:

#include<iostream>
#include<set>

using namespace std;

int father[10001];

int findFather(int x);
void unionTwo(int a, int b);

int main(){
	int N;
	scanf("%d", &N);
	for(int i = 1; i <= N; i++){
		father[i] = i;
	}
	char operation[2];
	int c1, c2;
	while(true){
		scanf("%s", operation);
		if(operation[0] == 'C'){
			scanf("%d %d", &c1, &c2);
			if(findFather(c1) == findFather(c2)){
				printf("yes\n");
			}else{
				printf("no\n");
			}
		}else if(operation[0] == 'I'){
			scanf("%d %d", &c1, &c2);
			unionTwo(c1, c2);
		}else{
			break;
		}
	}
	set<int> fatherSet;
	for(int i = 1; i <= N; i++){
		fatherSet.insert(findFather(i));
	}
	if(fatherSet.size() == 1){
		printf("The network is connected.\n");
	}else{
		printf("There are %d components.\n", fatherSet.size());
	}
	return 0;
}

int findFather(int x){
	int a = x;
	while(x != father[x]){
		x = father[x];
	}
	while(a != father[a]){
		int z = a;
		a = father[a];
		father[z] = x;
	}	
	return x;
}

void unionTwo(int a, int b){
	int a_father = findFather(a);
	int b_father = findFather(b);
	if(a_father != b_father){
		father[a_father] = b_father;
	}
}

C++解题报告:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP 7 Data Structures and Algorithms by Mizanur Rahman English | 6 Jun. 2017 | ASIN: B01IF7NLDW | 340 Pages | AZW3 | 2.55 MB Key Features Gain a complete understanding of data structures using a simple approach Analyze algorithms and learn when you should apply each solution Explore the true potential of functional data structures Book Description PHP has always been the the go-to language for web based application development, but there are materials and resources you can refer to to see how it works. Data structures and algorithms help you to code and execute them effectively, cutting down on processing time significantly. If you want to explore data structures and algorithms in a practical way with real-life projects, then this book is for you. The book begins by introducing you to data structures and algorithms and how to solve a problem from beginning to end using them. Once you are well aware of the basics, it covers the core aspects like arrays, listed lists, stacks and queues. It will take you through several methods of finding efficient algorithms and show you which ones you should implement in each scenario. In addition to this, you will explore the possibilities of functional data structures using PHP and go through advanced algorithms and graphs as well as dynamic programming. By the end, you will be confident enough to tackle both basic and advanced data structures, understand how they work, and know when to use them in your day-to-day work What you will learn Gain a better understanding of PHP arrays as a basic data structure and their hidden power Grasp how to analyze algorithms and the Big O Notation Implement linked lists, double linked lists, stack, queues, and priority queues using PHP Work with sorting, searching, and recursive algorithms Make use of greedy, dynamic, and pattern matching algorithms Implement tree, heaps, and graph algorithms Apply PHP functional data structures and built-in data structures and algorithms About the Autho

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值