题目链接: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. .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间

    下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...

  2. 九度OJ 1177 查找 (模拟)

    题目1177:查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5659 解决:1667 题目描写叙述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共同拥有 ...

  3. unity调用安卓打包apk时的错误unable to convert classes into dex format

    出现这种问题一般是由于有重复的文件所致,看下unity报的错误那些文件重复了,把重复的文件删了即可 例如,将eclipse中的安卓工程bin\class导出jar包时,会将下面的.class文件打包, ...

  4. LeetCode——Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  5. web即时通讯2--基于Spring websocket达到web聊天室

    如本文所用,Spring4和websocket要构建web聊天室,根据框架SpringMVC+Spring+Hibernate的Maven项目,后台使用spring websocket进行消息转发和聊 ...

  6. 新手安装使用codeblocks

    很多第一次使用codeblocks的新手总会遇到很多的问题,首先作为一名新手应该下一个完整版的codeblocks,下载地址链接: http://pan.baidu.com/s/1dDxKeD7 密码 ...

  7. 超过lua上帝的语言

    上帝的语言(god)它是基于lua和RPP新一代编程语言 为什么需要它? 1.好多人不喜欢lua语法,god的语法更像C 2.god支持元编程.闭包.协程 3.凡是lua支持的特性god也支持,lua ...

  8. hdu 4884 TIANKENG’s rice shop(模拟)

    # include <cstdio> # include <algorithm> # include <cstring> # include <cstdlib ...

  9. hdu1023

    import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger fac(Big ...

  10. 体验安装金蝶K/3 Wise 13.0(图像)

    金蝶13.0它提供windows7支持,而数据库也升级到SQL server 2008,有许多功能上的改善和增强.原本在位置低版本号需要时间来管理此功能,因为有这个模块没有原因一直没能起来,现在,新版 ...