HDU1213 How Many Tables 并查集入门
来源:互联网 发布:怎么修改淘宝付款密码 编辑:程序博客网 时间:2024/06/05 20:50
题目链接:HDU1213
裸题,直接套用模板
AC代码:
/*并查集入门2017年8月27日20:49:13HDU1213AC */#include <iostream>#include <map>#include <set>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector> using namespace std;const int maxn=1010;int pre[maxn];int n,m,a,b,ans;int find(int x){ int r=x; while(pre[r]!=r) r=pre[r];//查找到根节点为止 int i=x,j; while(i!=r){ j=pre[i]; pre[i]=r; i=j; } return r; } void join(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) pre[fx]=fy; }void init(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) pre[i]=i; for(int i=1;i<=m;i++){scanf("%d%d",&a,&b);join(a,b);} }void solve(){ans=0;for(int i=1;i<=n;i++){if(find(i)==i) ans++;}printf("%d\n",ans);}int main(){int t;scanf("%d",&t);while(t--){init();solve();} return 0;}
阅读全文
0 0
- HDU1213 How Many Tables 并查集入门
- 【hdu1213】How Many Tables【并查集】
- How Many Tables hdu1213 并查集
- hdu1213(How Many Tables 并查集)
- Hdu1213 - How Many Tables - 并查集
- HDU1213 How Many Tables 【并查集】
- HDU1213 How Many Tables【并查集】
- HDU1213 How Many Tables 【并查集】
- hdu1213 How Many Tables(并查集)
- How Many Tables--hdu1213(并查集)
- HDU1213-How Many Tables 并查集
- HDU1213 How Many Tables - 并查集
- hdu1213 How Many Tables 并查集
- HDU1213 How Many Tables(并查集)
- HDU1213 How Many Tables 并查集
- hdu1213 How Many Tables 并查集
- HDU1213 How Many Tables(并查集)
- hdu1213 How many tables 并查集
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- LeetCode题目分类
- Android第三天
- C++之成员函数内存布局③---补充(13)《Effective C++》
- Elasticsearch检索分类深入详解—基础篇
- HDU1213 How Many Tables 并查集入门
- 导航 4
- 有名信号量实现读者-写着问题(写者优先)
- 基于aiohttp web框架的微信公众号开发(项目)
- Leetcode--338. Counting Bits
- git的push与fetch
- HTTP协议详解
- Linux-视频监控系统(12)-移植到树莓派中
- OpenCV文档翻译--相机标定