
并查集
文章平均质量分 85
别动我的白羊毛
这个作者很懒,什么都没留下…
展开
-
hdu1272 小希的迷宫
#include#include#include#include#includeusing namespace std;int fa[100005],vis[100005];int find(int x){ while(fa[x]!=x){ x=fa[x]; } return x;}int main(){ int u,v; while(scanf("%d%d"原创 2017-09-20 13:51:24 · 153 阅读 · 0 评论 -
hdu1856 More is better
#include#include#include#include#includeusing namespace std;int fa[10000005],num[10000005];/*int find(int x){ while(fa[x]!=x){ x=fa[x]; } return x;}*/int find(int x){ //我用上面的方法求父亲就会T原创 2017-09-20 16:01:29 · 146 阅读 · 0 评论 -
hdu1232 畅通工程
一直以为这题要用最小生成树做,弄来弄去没弄出来,后来才知道用并查集就可以了#include#include#include#include#includeusing namespace std;int n,m,fa[1005];int find(int p){ if(fa[p]!=p) fa[p]=find(fa[p]); return fa[p];}void u原创 2017-09-20 18:46:27 · 153 阅读 · 0 评论 -
hdu 1598 find the most comfortable road
路径从小到大排序,然后开始枚举边,构建最小生成树,如果包含了要求的起点和终点,即 father【u】==father【v】 ,求得此时的差值,取个最小值就是了#include#include#include#include#include#define inf 999999999using namespace std;struct edge{ int u,v,w;原创 2017-09-22 16:06:18 · 157 阅读 · 0 评论 -
hdu1198 Farm Irrigation
一开始没有dfs,只是遍历一遍然后看上下左右,发现不行,还是需要dfs一下的本质还是并查集,不过就是合并的前奏长了点#include#include#include#include#includeusing namespace std;int father[500000];int vis[550][550];int map[100][4];//0.1.2.3分别表原创 2017-09-22 14:27:58 · 174 阅读 · 0 评论 -
带权并查集
来自:http://blog.youkuaiyun.com/Chris_zzj/article/details/52227656并查集并查集是一个很高效算法,理解起来也很简单,写起来更简单。①fat[i] = i;②找到一个点的祖先int findfat(int x){ if(fat[x] == x) return x; return findfat(fat[转载 2017-10-23 17:39:18 · 222 阅读 · 0 评论 -
Never Wait for Weights(带权并查集)
题意:遇到 !a b w 表示 b比a 重w,遇到? a b 表示输出a与b的w 的差值#include#include#include#include#include#define N 100005using namespace std;int fa[N],val[N];int find(int x){ if(fa[x] == x) return原创 2017-10-23 18:55:44 · 299 阅读 · 0 评论 -
HihoCoder - 1515 分数调查(带权并查集)
#1515 : 分数调查时间限制:10000ms单点时限:1000ms内存限制:256MB描述小Hi的学校总共有N名学生,编号1-N。学校刚刚进行了一场全校的古诗文水平测验。 学校没有公布测验的成绩,所以小Hi只能得到一些小道消息,例如X号同学的分数比Y号同学的分数高S分。 小Hi想知道利用这些消息,能不能判断出某两位同学之间的分数高低?输入第一行包含三个整...原创 2018-07-22 17:15:11 · 187 阅读 · 0 评论 -
Gym - 101879C C. Promenade by the lake(欧拉回路,dfs)
题目链接:Gym - 101879C C. Promenade by the lakeThe city of Porto will host the ICPC World Finals in 2019. One of the secret touristic spots in the city is the so-called "lake of the thousand bridges". M...原创 2018-09-27 18:51:54 · 407 阅读 · 0 评论