题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

简单并查集,统计单独成树的数量。

代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <map>
#include <stdlib.h>
using namespace std; int f[];
int n,m; void init()
{
for(int i=;i<=n;i++)
f[i]=i;
} int find(int x)
{
if(x==f[x])
return f[x];
f[x]=find(f[x]);
return f[x];
} void Union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b)
return;
f[a]=b;
} int main()
{
int t,a,b;
while(cin>>t){
while(t--){
scanf("%d %d",&n,&m);
init();
int sum=;
for(int i=;i<m;i++){
scanf("%d %d",&a,&b);
Union(a,b);
}
for(int i=;i<=n;i++){
if(i==find(i)){
sum++;
}
}
printf("%d\n",sum);
}
}
}

1213 How Many Tables(简单并查集)的更多相关文章

  1. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  2. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  3. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

  4. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  5. hdu 1213 How Many Tables(并查集练习)

    题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...

  6. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  7. HDU 1213 How Many Tables(并查集)

    传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...

  8. HDU 1213 How Many Tables(并查集裸题)

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  9. How Many Tables 简单并查集

    Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...

  10. HDU 1213 How Many Tables【并查集】

    解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. js右侧悬浮框

    示例:屏幕右侧悬浮框 原理:oDiv.style.top = document.documentElement.clientHeight - oDiv.offsetHeight + scrollTop ...

  2. ListView滑动删除 ,仿腾讯QQ

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/22961279 在CSDN上开了很多大神们的文章,感觉受益良多,也非常欣赏大家的分 ...

  3. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. 开源备份软件bacula安装记录--包括备份-恢复演练

    该公司原先使用的备用机oracle-linux 5.7 不是很稳定执行.经常死机,新安装centos6.5, 即用bacula要备份的数据.在这里,有关安装故障记录,MEMO. 操作系统:centos ...

  5. 指定为文件头部Expires要么Cache-Control

    Cache-Control :private,no-cache.must-revalidate,max-age Expires指定页面过期时间 response.setContentType(&quo ...

  6. POJ 3177 Redundant Paths - from lanshui_Yang

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  7. UIStepper使用的具体解释的控制

    UIStepper控件类似于UISlider控件,但它有"+"和"-"两个button,单击当中一个可使属性value值递增或递减. 如声音.速度.图片等的大小 ...

  8. UVA No Tipping

    Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm,it will ...

  9. asm 盘头损失,破坏

    BUG 14693394 – ORA-15196: INVALID ASM BLOCK HEADER [KFC.C:26076] [ENDIAN_KFBH] BUG 14758001 – ORA-15 ...

  10. js的StringBuffer实施和使用类

    <strong>JAVA有一个StringBuffer分类,js但不是在下面,以实现自己的简单js的StringBuffer分类.</strong> //创建一个StringB ...