HDU 1213 How Many Tables
来源:互联网 发布:安卓魔音通话变声软件 编辑:程序博客网 时间:2024/06/04 00:47
这是我刚刷的第一道并查集的题,纪念一下!!!!!!!!!
#include<iostream>
#include<cstdio>#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int father[1232];
int findx(int x) {
int r=x;
while(father[r]!=r) {
r=father[r];
}
return r;
}
void merge(int x,int y) {
int fx=findx(x);
int fy=findx(y);
if(fx!=fy) {
father[fx]=fy;
}
}
int n,m;
void init() {
for(int i=1; i<=n; i++) {
father[i]=i;
}
}
int main() {
int T;
while(~scanf("%d",&T)) {
while(T--) {
scanf("%d%d",&n,&m);
init();
int x;
int y;
while(m--) {
scanf("%d%d",&x,&y);
merge(x,y);
}
int ans=0;
for(int i=1; i<=n; i++) {
if(father[i]==i) {
ans++;
}
}
printf("%d\n",ans);
}
}
return 0;
}
0 0
- hdu 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- Hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213how many tables
- hdu 1213 How Many Tables
- Hdu 1213 - How Many Tables
- HDU 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- NYOJ 题目3 多边形重心问题
- error: invalid deployment target '6.1.0' for architecture 'arm64' (requires '7.0.0' or later)问题的解决方法
- BLUEPRINT
- Java中的null布局
- VS2012 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPack
- HDU 1213 How Many Tables
- 去除vim ^M符号
- 【动态规划】作业题
- ubuntu13.04下配置hadoop-1.2.1伪分布式环境
- python之一(简明python教程新旧版勘误)
- python中关于import语句的认识
- 常数变易法_打印图形
- F 求最长公共字串 hash+二分
- java全排列