并查集
文章平均质量分 92
介绍并查集的概念、算法实现和题目,以及拓展并查集
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 2316. 统计无向图中无法互相到达点对数【图,BFS,DFS,并查集】1604
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-22 15:20:36 · 180 阅读 · 0 评论 -
LeetCode 1254. Number of Closed Islands【DFS,BFS,并查集】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-19 12:05:52 · 708 阅读 · 0 评论 -
【PAT甲级】1013 Battle Over Cities (25 分)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the c原创 2021-05-29 04:00:54 · 159 阅读 · 1 评论 -
LeetCode C++ 1319. Number of Operations to Make Network Connected【BFS/DFS/Union Find】中等
There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. A原创 2020-12-09 01:23:17 · 202 阅读 · 0 评论 -
LeetCode C++ 200. Number of Islands【DFS/BFS】中等
Given an m x n 2d grid map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded b原创 2020-11-24 17:55:56 · 191 阅读 · 0 评论 -
LeetCode C++ 684. Redundant Connection【Tree/Graph/Union Find/TopologicalSort】中等
In this problem, a tree is an undirected graph that is connected and has no cycles.The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N ), with one additional edge added. The added edge has two different vertic原创 2020-09-17 23:45:05 · 202 阅读 · 0 评论 -
【算法学习】高级数据结构2 种类并查集
文章目录1. 种类并查集2. 实际例题1. 种类并查集如果说一般的并查集,维护的是等价、连通关系,例如朋友的朋友是朋友。那么种类并查集,维护的就是对立关系:敌人的敌人是朋友,或者更宽泛的说,是多个种类集合间的一种循环对称的关系。常见的做法是将原并查集扩大一倍规模,并划分为两个种类。在同种类的并查集中合并,和原始的并查集没什么区别,仍然表达 他们是朋友 这个含义。在不同种类的并查集中进行合并,表达的则是 他们是敌人 这个含义。例如,要维护 4 个元素的种类并查集,要开 8 个单位的空间:用 1-原创 2020-08-29 02:21:23 · 394 阅读 · 0 评论 -
POJ 2236 Wireless Network【并查集】
DescriptionAn earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are re原创 2020-08-29 19:57:50 · 165 阅读 · 0 评论 -
【算法学习】高级数据结构1 并查集
文章目录1. 并查集操作的简单实现(1) 初始化(2) 合并(3) 查找(4) 统计集合个数2. 题目示例并查集/不相交集(Disjoint Set)是一种巧妙而实用的数据结构,主要用于处理一些不相交集合的合并问题,是等价类的实际应用。经典的示例用连通子图、最小生成树、最近公共祖先(Lowest Common Ancestors, LCA)等等。并查集将编号为 1~n 的 nnn 个对象分为不相交的集合,在这个集合中,并查集的操作有初始化,合并,查找。下面给出并查集的简单实现。1. 并查集操作的简单实原创 2020-08-28 02:22:15 · 270 阅读 · 0 评论 -
洛谷 P3367 【模板】并查集
不错过任何一道模板题!原创 2020-08-28 02:20:16 · 166 阅读 · 0 评论 -
洛谷 P3958 奶酪【并查集】
题目描述现有一块大奶酪,它的高度为 hhh,它的长度和宽度我们可以认为是无限大的,奶酪 中间有许多 半径相同 的球形空洞。我们可以在这块奶酪中建立空间坐标系,在坐标系中, 奶酪的下表面为 z=0z=0z=0 ,奶酪的上表面为 z=hz=hz=h 。现在,奶酪的下表面有一只小老鼠 Jerry,它知道奶酪中所有空洞的球心所在的坐 标。如果两个空洞相切或是相交,则 Jerry 可以从其中一个空洞跑到另一个空洞,特别地,如果一个空洞与下表面相切或是相交,Jerry 则可以从奶酪下表面跑进空洞;如果一个空洞与上表原创 2020-06-10 13:29:11 · 379 阅读 · 0 评论 -
洛谷 P1551 亲戚【并查集】
题目背景若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。题目描述规定:x 和 y 是亲戚,y 和 z 是亲戚,那么 x 和 z 也是亲戚。如果 x,y 是亲戚,那么 x 的亲戚都是 y 的亲戚,y 的亲戚也都是 x 的亲戚。输入格式第一行:三个整数 n,m,pn,m,pn,m,p,(n<=5000,m<=5000,p<=5000),分别表示有 n 个人,m 个亲戚关系,询问 p 对亲戚关系。以下 m 行:原创 2020-06-10 02:02:21 · 283 阅读 · 0 评论 -
POJ 2524 Ubiquitous Religions【并查集】
DescriptionThere are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.You know that there are n students i原创 2020-06-09 15:48:53 · 235 阅读 · 0 评论 -
POJ 1611 The Suspects【并查集】
DescriptionSevere acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.In the Not原创 2020-06-09 14:46:53 · 276 阅读 · 0 评论 -
HDU How Many Tables【并查集】
Problem DescriptionToday is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to s原创 2020-06-08 20:52:37 · 229 阅读 · 0 评论 -
LeetCode C++ 990. Satisfiability of Equality Equations【并查集】中等
Given an array equations of strings that represent relationships between variables, each string equations[i] has length 444 and takes one of two different forms: "a==b" or "a!=b". Here, a and b are lowercase letters (not necessarily different) that repres原创 2020-06-08 17:01:46 · 224 阅读 · 0 评论