前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >畅通工程-HDU-1232(并查集经典模板)

畅通工程-HDU-1232(并查集经典模板)

作者头像
Cell
发布2022-02-25 14:46:28
1930
发布2022-02-25 14:46:28
举报
文章被收录于专栏:Cell的前端专栏
并查集入门推荐:超有爱的并查集~
题目链接:畅通工程
题意分析:

首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的。最后要解决的是整幅图的连通性问题。比如随意给你两个点,让你判断它们是否连通,或者问你整幅图一共有几个连通分支,也就是被分成了几个互相独立的块。像畅通工程这题,问还需要修几条路,实质就是求有几个连通分支。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include<iostream> #include<cstdio> using namespace std; int pre[1010]; int findd(int root){ int son,t; son=root; while(root!=pre[root]) root=pre[root]; while(son!=root){ t=pre[son]; pre[son]=root; son=t; } return root; } int main(){ int n,m,i,sum,r1,r2,star,end1; while(scanf("%d",&n)&&n){ sum=n-1; for(i=1;i<=n;i++) pre[i]=i; scanf("%d",&m); while(m--){ scanf("%d%d",&star,&end1); r1=findd(star); r2=findd(end1); if(r1!=r2){ pre[r1]=r2; sum--; } } printf("%d\n",sum); } return 0; }

基础回顾:
find() 函数找根结点的两种写法如下:

第一种递归:

1 2 3 4

int find(int x) { return x == pre[x] ? x : find(pre[x]); }

第二种:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

int find(int x) { int root, temp; root = x; while(root != pre[root]) root = pre[root]; while(x != root) { temp = pre[x]; pre[temp] = root; x = temp; } return root; }

合并函数

1 2 3 4 5 6

void join(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) pre[fx]=fy; }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 并查集入门推荐:超有爱的并查集~
  • 题目链接:畅通工程
  • 题意分析:
  • 基础回顾:
    • find() 函数找根结点的两种写法如下:
      • 合并函数
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档