首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >how many tables-HDU-1213(并查集求连通域数目)

how many tables-HDU-1213(并查集求连通域数目)

作者头像
Cell
发布2022-02-25 14:42:48
发布2022-02-25 14:42:48
4120
举报
文章被收录于专栏:Cell的前端专栏Cell的前端专栏

并查集求连通域数目,初始化 sum=n; 题目链接: how many tables

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 40

#include<bits/stdc++.h> using namespace std; int pre[1005]; int find(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,t,sum,root1,root2; cin>>t; while(t--){ cin>>n>>m; sum=n; for(int i=1;i<=n;i++) pre[i]=i; for(int i=0;i<m;i++){ cin>>root1>>root2; int xx=find(root1); int yy=find(root2); if(xx!=yy){ pre[xx]=yy; sum--; } } cout<<sum<<endl; } return 0; }

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档