题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌。简单的并查集应用,但注意题意是从1到n的,所以要减1。

代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<(b);i++)
#define rank mrank
const int MAX_BN=1005;
int par[MAX_BN];// father
int rank[MAX_BN],n,m;// deep void init(int n)
{
for (int i=0;i<n;i++)
{
par[i]=i;
rank[i]=0;//并查集是从0开始的 高度也是从0开始的
}
}
//查询树的根
int myFind(int x)
{
if (par[x]==x)
{
return x;
}
else{
return par[x]=myFind(par[x]);
}
}
//合并x和y所属的集合
void unite(int x,int y)
{
x=myFind(x);
y=myFind(y);
if (x==y)
return; if (rank[x]<rank[y])
{
par[x]=y;
}
else
{
par[y]=x;
if (rank[x]==rank[y]) rank[x]++;
}
}
//判断x和y是否属于同一集合
bool same(int x,int y)
{
return myFind(x)==myFind(y);
}
int a,b;
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE
int o;
SI(o);
while(o--)
{
SII(n,m);
init(n);
rep(i,m)
{
SII(a,b);
unite(a-1,b-1);
}
int ans=0;
rep(i,n)
if (par[i]==i)
ans++;
cout<<ans<<endl;
}
return 0;
}

  

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

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  4. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  5. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  6. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  7. 题解报告:hdu 1232 畅通工程(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了 ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

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

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

随机推荐

  1. Sublime Text 转

    距第一篇的开箱水文,已经有4个月的时间了,但因为懒,就没有下文了.终于,今天,我觉得写一篇准技术文章了. 忘记了是怎么开始用的ST,应该是在网上看到别人推荐才用到吧,用了有半年了.在windows下是 ...

  2. Lua5.1基本函数库介绍

    Lua5.1基本函数库介绍assert (v [, message])功能:相当于C的断言,参数:v:当表达式v为nil或false将触发错误,message:发生错误时返回的信息,默认为" ...

  3. JAVA 新闻

    Oracle已对Java失去兴趣?Java社区能否扭转乾坤? http://news.cnblogs.com/n/549566/ http://mp.weixin.qq.com/s?__biz=MjM ...

  4. 【转】Python numpy库的nonzero函数用法

    当使用布尔数组直接作为下标对象或者元组下标对象中有布尔数组时,都相当于用nonzero()将布尔数组转换成一组整数数组,然后使用整数数组进行下标运算. nonzeros(a) 返回数组a中值不为零的元 ...

  5. linux日志处理logrotate使用

    摘录自:http://linux008.blog.51cto.com/2837805/555829 内容在这里做个备份,以便以后查看:  使用logrotate管理nginx日志文件 2011-04- ...

  6. java_常用数据类型转换基础篇

    一.java基本数据类型 1.java基本数据类型可分四类八中 第一类:整形:byte.short.int.long 第二类:浮点型:float(单精度) .double(双精度) 第三类:逻辑类型: ...

  7. linux服务之drbd

    http://www.drbd.org/docs/about/http://oss.linbit.com/drbd/ 一般我们会在生产环境的MYSQL中用drbd +ha做master 备份,当然这是 ...

  8. wikioi 1430 素数判定

    /*====================================================================== 题目描述 Description 质数又称素数.指在一 ...

  9. docker nodejs 基本应用

    1. 安装docker 环境 2. nodejs  应用布局 package.json { "name": "docker-centos-hello", &qu ...

  10. xunsearch安装配置

    1.wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 2.tar -xjf xunsearch-full-lat ...