题目链接: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. android存储阵列数据SharedPreferences

    假设要数组数据(如boolean[] .int[]等)到SharedPreferences时,我们能够先将数组数据组织成json数据存储到SharedPreferences,读取时则对json数据进行 ...

  2. 自定义DB连接池实现

    实现一个简单的数据库连接池 1,连接池接口 package dbsource; import java.sql.Connection; import java.sql.SQLException; /* ...

  3. 《TCP/IP详细解释》札记(23章)-TCP该保活定时器

    可能有这样的备用现实TCP连接:流通过. 也就是说.假设TCP连接的两方都没有向对方发送数据.则在两个TCP模块之间不交换不论什么信息,这意味着我们能够启动一个客户与server建立连接,然后长时间不 ...

  4. 公钥\私人 ssh避password登陆

    相关概念以前见过,决不要注意,使用公共密钥管理之前,腾讯云主机的备案机,非常头发的感觉,查了一下相关资料,这里总结下: 字符a:192.168.7.188 (ubuntu) 字符b:192.168.7 ...

  5. 假设synthesize省略,语义属性声明assign retain copy时间,为了实现自己的setter和getter方法

    假设synthesize省略,而且我们自己实现setter和getter方法时,系统就不会生成相应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和g ...

  6. Scrapy系列教程(2)------Item(结构化数据存储结构)

    Items 爬取的主要目标就是从非结构性的数据源提取结构性数据,比如网页. Scrapy提供 Item 类来满足这种需求. Item 对象是种简单的容器.保存了爬取到得数据. 其提供了 类似于词典(d ...

  7. Android异步操作总结

    Android中常常会有一些操作比方网络请求,文件读写.数据库操作.比較耗时,我们须要将其放在非UI线程去处理.此时.我们须要处理任务前后UI的变化和交互.我们须要通过类似js中异步请求处理,这里总结 ...

  8. Hibernate 学习教程

    第1课 课程内容. 6 第2课Hibernate UML图. 6 第3课 风格. 7 第4课 资源. 7 第5课 环境准备. 7 第6课 第一个演示样例HibernateHelloWorld 7 第7 ...

  9. HDU 1710-Binary Tree Traversals(二进制重建)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  10. design pattern Builder 生成器设计模式

    其实设计模式可以学习很有趣,你并不需要有这么难啃旱地FOG对我来说,当然,这些都是健康的骨骼啃啃. 在本文中,建造者模式设计一个搞笑的一幕.根据这一模型来学习功夫的方法,哈哈. 基类的第一,设计.那么 ...