PAT甲级 并查集
PAT甲级中的并查集问题
爱吃梦龙雪糕
这个作者很懒,什么都没留下…
展开
-
1107 Social Clusters (30 分)
本题考察并查集的实现。 并查集即合并,查找,集合的缩写 坑点如下 1,集合的表示方法,由father数组来记录一个结点的父亲结点。 2,查找根结点的函数 注意,else return必须写,否则整个函数无返回值。数据也就无法更新 int findfather(int x){ if(father[x]==x) {return x;} else return findfather(father[x]); } 3,集合的合并,将元素a,b合并至同一集合,应将其各自的根结点合并 void原创 2021-08-09 16:46:28 · 46 阅读 · 0 评论 -
1114 Family Property (25 分)
这题用到了并查集的思想,我的代码参考了柳婼的代码。柳神代码链接 坑点如下 1,统计人数时不能只遍历输入的id,还要遍历与之相关的人,因此开vis数组,记录所有出现的人。 2,比较函数注意要写return(在这一点上卡了很久) 3,注意find函数写法,不建议使用递归,易错。 int find(int x){ while(father[x]!=x){ x=father[x]; } return x; } 整体代码如下 #include<bits/stdc++.h>原创 2021-08-19 13:09:34 · 100 阅读 · 0 评论 -
1118 Birds in Forest (25 分)
并查集问题,考察的点有,集合的合并,集合个数的计算,各集合中元素数量统计。 坑点如下 1,在函数内定义数组时注意要赋值0。 整体代码如下 #include<bits/stdc++.h> using namespace std; int father[10010]; int vis[10010]={false}; int cnt[10010]; int n,q; int find(int x){ while(father[x]!=x){ x=father[x]; }原创 2021-08-20 09:38:52 · 45 阅读 · 0 评论