并查集是一种可以在较短的时间内进行集合的查找与合并的树形数据结构 每次合并只需将两棵树的根合并即可 通过路径压缩减小每颗树的深度可以使查找祖先的速度加快不少 代码如下: int getfather(int x) //查找祖先 { if(father[x]!=x) father[x]=getfather(father[x]); //路径压缩,把每个节点的父亲都变得与他的祖先相同 else return x; return father[x]; } int issame(int x,int y) /…
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <vector&g…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #define maxn 1100 using namespace std; int parent[maxn]; int m,n; int Find(int x) { int s; ;s=pa…
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31632 Accepted Submission(s): 15706 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner ti…
Work HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2109 Accepted Submission(s): 1236** Problem Description It's an interesting experience t…
传送门 一个朴素的做法就是暴力连边并查集,可是这是\(O(n^2)\)的.发现每一次连边可以看成两个区间覆盖,这两个区间之间一一对应地连边.可线段树对应的两个节点的size可能不同,这会导致"一一对应"的条件在线段树上失效.所以我们需要使用ST表来完成连边. 对原序列建好ST表,对于每一个修改将两个区间覆盖到ST表上然后两两之间连边.注意在ST表上连边的两个区间要对应,即如果ST表上对应\([l,r]\)的区间与对应\([L,R]\)的区间连了边,意味着对于\(\forall i \i…
传送门 最少好要修多少条路太能使全部城镇连通.只要用并查集算可以连通的城市的组数,修的路就是组数减1 #include<bits/stdc++.h> using namespace std; #define C getchar() template <typename T> inline void read(T &s){ T t=; ; ;//判断该数正负 )+(s<<)+(k^);//<<1加上<<3就相当于*10,但是位运算的速度较快…
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 并查集 图同构 连通分量.cpp * @Platform: Windows * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/st…
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23551    Accepted Submission(s): 7234 Problem Description 上 次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是…
http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 48102    Accepted Submission(s): 14966 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在…