并查集
文章平均质量分 65
新西兰做的饭
Here we go!
展开
-
PTA-1114 Family Property(并查集)
PTA-1114 Family PropertyThis time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and原创 2021-05-31 19:30:53 · 85 阅读 · 0 评论 -
PTA-1118 Birds in Forest(并查集)
PTA-1118 Birds in ForestSome scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the fore原创 2021-05-28 19:17:09 · 94 阅读 · 0 评论 -
PTA-1107 Social Clusters(并查集)
PTA1107-Social ClustersWhen register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are sup原创 2021-05-27 20:30:33 · 84 阅读 · 0 评论 -
POJ-1182 食物链(并查集)
传送门 食物链动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。有人用两种说法对这N个动物所构成的食物链关系进行描述:第一种说法是"1 X Y",表示X和Y是同类。第二种说法是"2 X Y",表示X吃Y。此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。1) 当前原创 2021-05-26 17:11:37 · 88 阅读 · 0 评论 -
POJ-1703 Find them,Catch them(并查集)
传送门The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is,原创 2021-05-25 19:00:23 · 115 阅读 · 0 评论 -
LeetCode130:被围绕的区域(DFS+BFS+并查集)
被围绕的区域原创 2021-05-12 19:50:48 · 121 阅读 · 1 评论 -
LeetCode765:情侣牵手(并查集)
情侣牵手解决代码:class Solution {public: vector<int> a; vector<int> h; void unionfind(int n) { for(int i=0;i<n;i++) { a.push_back(i); h.push_back(0); } } int find(int x)原创 2021-05-01 23:51:33 · 160 阅读 · 0 评论 -
LeetCode685:冗余连接II(并查集)
冗余连接II相比于冗余连接1把无向图换成有向图,所以需要分类讨论。有环边:测试样例:[[1,2],[2,3],[3,1]]输出:[3,1]有冲突测试样例:[[1,2],[1,3],[2,3]]输出:[2,3]显然,对于以上两种情况,只存在1.有环边无冲突2.无环边有冲突第一种情况成环的边显然是最后一个输入的边第二种情况冲突的边就是使一个节点有了第二个父亲节点的边这两种情况只需要存储那条边并输出即可。有环边有冲突测试样例:[[2,1],[3,1],[4,2],[1,原创 2021-05-01 00:27:28 · 135 阅读 · 1 评论 -
LeetCode684:冗余连接(并查集)
冗余连接提交代码:class Solution {public: vector<int >a; vector<int >h; void unionfind(int n) { for(int i=0;i<=n;i++) { a.push_back(i); h.push_back(0); } } int find(int x)原创 2021-04-29 20:16:02 · 92 阅读 · 0 评论 -
LeetCode547:省份数量(并查集)
省份数量解决代码:class Solution {public: vector<int> a; vector<int> h; int num=0; void unionfind(int n) { for(int i=0;i<n;i++) { a.push_back(i); h.push_back(0); num++;原创 2021-04-29 19:59:22 · 99 阅读 · 0 评论 -
LeetCode947:移除最多的同行或同列石头(并查集)
移除最多的同行或同列石头解决代码:class Solution {public: vector<int >a; vector<int >h; int num=0; void unionfind(int n) { for(int i=0;i<n;i++) { a.push_back(i); h.push_back(0); } }原创 2021-04-29 19:48:55 · 170 阅读 · 0 评论 -
LeetCode200 :岛屿数量(DFS+BFS+并查集)
LeetCode200.岛屿数量这是一道求连通块分支个数的题,是一道dfs模板题。DFSclass Solution {public: int m,n;//m行n列 bool board(int x,int y) { if((x>=0)&&(y>=0)&&(x<n)&&(y<m)) { return true; }原创 2021-04-28 15:09:12 · 229 阅读 · 0 评论 -
蓝桥杯-修改数组
题目描述修改数组题解:利用常规方法遍历复杂度过大,通过分析题目依次累加1可以想到利用并查集的树形结构进行查找赋值。并查集的构建-详解并查集首先初始化10的六次方个节点,接着开始输入,每次查找输入元素的根并将根的值赋值给a[i],接着更新a[i]的根+1:再次访问时根植大1,这样就会不断向上爬,利用树形结构直接查找并赋值。代码如下:#include<iostream>using namespace std;const int maxx=1000010;int par[ma原创 2021-03-27 14:38:17 · 136 阅读 · 0 评论 -
蓝桥杯-合根植物
题目描述合根植物题解:这是一道典型的求连通分支数的问题,可以想到利用并查集。并查集的构建-详解并查集首先初始化m*n个元素,并合并题目中相连接的元素,接着通过查找根元素将相同根元素(属于同一集合)的元素标记:令a[m*n]数组中根元素下标的位置置为1,。最后遍历统计数组中置为1的个数即可。代码如下:#include<iostream>using namespace std;const int maxx=1000010;int par[maxx];int ran[原创 2021-03-27 13:45:28 · 85 阅读 · 0 评论 -
图文详解并查集
并查集并查集一、初始化函数init()二、合并操作unite()三、查询树根函数find()四、改进后的find()五、判断是否属于同一集合same()总结测试代码及运行实例并查集并查集是一种用来管理元素分组情况的数据结构,可以高效地进行查询、合并操作,但无法进行分割操作。1.查询元素a和元素b是否属于同一组2.合并元素a和元素b所在的组并查集使用树形结构实现,但不是二叉树。在并查集中,无需关注元素的兄弟、父亲是谁或树的结构如何,只需要关注元素的祖先。一、初始化函数init().原创 2021-03-24 18:28:12 · 603 阅读 · 0 评论