
并查集
SYaoJun
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode并查集问题汇总
模板 static const int N = 1e5+5; int p[N]; void init() { for(int i = 0; i < N; i++) p[i] = i; } int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } void union_(int x, int y) { int fx =原创 2021-12-04 23:23:10 · 340 阅读 · 0 评论 -
P10 推断学生所属学校的人数 (15 分)
PTA题目链接 并查集 求并查集的集合数量,以及所有集合中数量最大的值。 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <unordered_map> using namespace std; const int ...原创 2019-11-04 13:32:14 · 1291 阅读 · 0 评论 -
859. Kruskal算法求最小生成树
AcWing题目链接 Kruskal算法 把所有边按照边权从小到大排序 把边权最小的两个顶点使用并查集加入同一个集合 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int M = 2e5+5; int root[M]; struct...原创 2019-10-30 22:47:03 · 132 阅读 · 0 评论 -
7-36 社交网络图中结点的“重要性”计算 (30 分)
层次遍历BFS 并查集判断 #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> using namespace std; const int N = 1e...原创 2019-10-20 14:34:44 · 594 阅读 · 0 评论 -
Jungle Roads【北京大学】
题目描述 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relen...原创 2018-03-01 17:15:18 · 299 阅读 · 0 评论 -
7-32 哥尼斯堡的“七桥问题” (25 分)
无向图欧拉图的判断 1.连通 2.节点度数为偶数 注意: 如果数据量较大尽量用scanf来读入,不然就是会超时。 #include <iostream> #include <vector> #include <string> #include <cmath> #include <algorithm> #include <queue&...原创 2019-10-12 14:20:55 · 479 阅读 · 0 评论 -
L2-010 排座位 (25 分)
一道近乎裸并查集的题 需要注意的是保存敌对关系 #include <iostream> using namespace std; const int N = 107; int root[N]; int G[N][N]; int find(int x){ if(x == root[x]) return x; else return root[x] = find(root[x]); }...原创 2019-09-06 14:27:34 · 598 阅读 · 0 评论 -
L3-003 社交集群 (30 分)
跟着书上学的,确实不太好理解。 代码中用到了并查集合lambda表达式 还是要多刷题 #include <iostream> #include <algorithm> #include <string> #include <deque> #include <cstring> #include <queue> #include ...原创 2019-09-07 22:09:09 · 597 阅读 · 0 评论 -
836. 合并集合
并查集 #include <iostream> #include <algorithm> using namespace std; const int N =1e5+7; int root[N]; int n, m; int find(int x){ if(root[x] == x) return x; return root[x] = find(root[x]...原创 2019-09-11 16:26:48 · 541 阅读 · 0 评论 -
837. 连通块中点的数量
#include <iostream> #include <algorithm> using namespace std; const int N =1e5+7; int p[N], c[N]; int n, m; int find(int x){ if(x != p[x]) p[x] = find(p[x]); return p[x]; //肯定不能是x啊 因...原创 2019-09-11 16:38:33 · 177 阅读 · 0 评论 -
7-50 畅通工程之局部最小花费问题 (35 分)
并查集 kruskal算法的应用 需要先将连通的点合并起来,然后再查询。 由于集合的原因,肯定不会包含重复的点。 #include <iostream> #include <cstdio> #include <string> #include <unordered_map> #include <algorithm> using names...原创 2019-09-26 16:18:47 · 693 阅读 · 0 评论 -
畅通工程【浙江大学】★
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村...原创 2018-03-01 17:16:58 · 248 阅读 · 0 评论 -
继续畅通工程【浙江大学】★★
题目描述 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1 当N为0时输入结束。原创 2018-01-30 15:01:13 · 344 阅读 · 0 评论 -
7-3 Telefraud Detection (25 分)
这道题可把我搞惨了 我自己踩的坑,大家可以借鉴一下 1.短电话的时长,需要加起来,因为存在两个不止一次通话。 2.只有是短电话的人回电才计数。 3.集合要用并查集 AC 25分 #include <iostream> #include <vector> #include <string> #include <cmath> #include <a...原创 2019-10-04 14:46:11 · 253 阅读 · 1 评论 -
684. Redundant Connection
class Solution { public: int p[1005]; int find(int x){ if(p[x] != x) p[x] = find(p[x]); return p[x]; } vector<int> findRedundantConnection(vector<vector<int...原创 2019-08-26 14:10:42 · 122 阅读 · 0 评论 -
7-8 File Transfer (25 分)
并查集入门题 注意: 最后遍历根的时候还要find一次 #include <iostream> #include <vector> #include <algorithm> #include <unordered_set> using namespace std; const int N = 1e4+7; int root[N]; int find(...原创 2019-09-02 20:39:17 · 308 阅读 · 0 评论 -
51nod 1212 无向图最小生成树
题目链接 kruskal算法 1.先对边权从小到大排序 2.每次挑选边权最小的两个顶点来判断,如果不在一个集合,就用并查集进行合并。 3.每次合并的时候记录权值,最后输出。 #include <iostream> #include <vector> #include <algorithm> #include <cstring> using name...原创 2019-09-02 10:38:54 · 151 阅读 · 0 评论 -
1118 Birds in Forest (25 分)
https://www.patest.cn/contests/pat-a-practise/1118 吐槽:这是道并查集的题,没有什么好说的,但是可以钻题目的空子还是要钻的,因为鸟的索引是连续的,所以鸟的总数就是其中最大的数,在输入的时候保存下来就行了,最后就是树的组数,主要把并查集构建起来,组数只要记录父节点等于自己本身的点。 #include #include #define N 1001原创 2018-01-27 00:44:22 · 292 阅读 · 0 评论 -
1107. Social Clusters (30)
https://www.patest.cn/contests/pat-a-practise/1107 吐槽:其实我所谓的吐槽就是把自己上过的当告诉大家,希望大家做题的时候认真点,别像我,错了这么多次才长记性。 这道题表面上是并查集的简单题,但是我的英语是真的烂,而且自己臆想了一段内容,本来是对人的并查,我搞成了对爱好的并查,最后怎么做都不对。 这个故事告诉我们一个道理,读懂题目是正确解答的第原创 2018-01-27 16:26:45 · 293 阅读 · 0 评论 -
More is better【HDOJ1856】
http://acm.hdu.edu.cn/showproblem.php?pid=1856 典型的并查集 #include #include #include #define maxn 10000005 using namespace std; int father[maxn]; int isRoot[maxn]; int mx; int findFather(int x){ if(原创 2018-01-27 22:02:47 · 253 阅读 · 0 评论 -
How Many Tables【HDOJ1213】
http://acm.hdu.edu.cn/showproblem.php?pid=1213 并查集模板 #include #include #include #define maxn 1005 using namespace std; int father[maxn]; int isRoot[maxn]; int findFather(int x){ if(father[x]原创 2018-01-27 17:15:39 · 296 阅读 · 0 评论 -
欧拉回路【浙江大学】★
//无向图的欧拉回路判断 //1.图连通 //2.所有顶点的度为偶数 //有向图的欧拉回路判断 //1.图连通 //2.图中所有节点入度等于出度 #include #define N 1005 int father[N]; int num[N]; int findFather(int x){ if(x == father[x]) return x; else{原创 2018-01-30 03:02:00 · 353 阅读 · 0 评论 -
Freckles【POJ2560】
题目描述 In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his...原创 2018-03-01 17:13:10 · 793 阅读 · 0 评论 -
1107 Social Clusters (30 分)
#include<cstdio> #include<cstring> #include<algorithm> #define N 1005 using namespace std; int father[N]; int isRoot[N]; int course[N]; int findFather(int x){ if(father[x] == x) ...原创 2018-03-01 17:16:00 · 188 阅读 · 0 评论 -
7-25 朋友圈 (25 分)
并查集经典入门题 虽然简单,但是我觉得我还是学到了很多。 首先,加深了我对并查集的理解。 每个集合合并的时候,总是把第一个值作为父节点是会产生错误的。 例如: 输入样例 7 4 3 1 2 3 2 4 2 3 5 6 7 1 6 输出样例 4 因此合并完之后,如果要考查每个结点属于哪个集合,还在再find一遍它压缩之后的根结点。 其次,这个代码参考了另一个大神的写法,其中包含很多有用的技巧,比如散...原创 2019-02-12 11:41:14 · 514 阅读 · 0 评论 -
L2-024 部落 (25 分)
并查集 需要注意的地方是并查集的路径压缩,最关键的东西是父节点数组,所有信息都存在这个上面。 这道题用了两个集合,第一个集合是用来存有多少个成员,第二个集合是用来存有多少个部落。其实也可用散列,但是集合的好处是可以直接输出集合的数量。 #include<bits/stdc++.h> #include<iostream> #include<set> using n...原创 2019-07-19 14:44:29 · 409 阅读 · 0 评论 -
1034 Head of a Gang (30 分)
并查集 还有三个测试点没过 20分 求指点 #include<bits/stdc++.h> using namespace std; const int maxn = 3005; int f[maxn],val[maxn]; map<int, string> IS; map<string, int> SI; int person=1; struct node{...原创 2019-07-25 16:26:50 · 195 阅读 · 0 评论 -
最小生成树模板
https://www.luogu.org/problemnew/show/P3366 #include #include using namespace std; const int maxn = 2000005; const int maxx = 5005; int root[maxx]; int find(int x){ return x==root[x]?x:root[x原创 2018-01-31 03:11:36 · 363 阅读 · 0 评论 -
还是畅通工程【浙江大学】★
题目描述 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应...原创 2018-03-01 17:17:42 · 219 阅读 · 0 评论 -
畅通工程【浙江大学】★
题目描述 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( 注意:两个城市之间可以有多条道路原创 2018-01-16 13:38:29 · 351 阅读 · 0 评论