HDU 1213 How Many Tables(并查集裸题)
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std; int p[];
int find(int i)
{
if(p[i]==i)
return i;
else
return find(p[i]);
} int main()
{
int T,n,m,a,b,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
p[i]=i;
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
x=find(a);
y=find(b);
if(x!=y)
p[x]=y;
}
int ans=;
for(int i=;i<=n;i++)
if(p[i]==i)
ans++;
printf("%d\n",ans);
}
return ;
}
HDU 1213 How Many Tables(并查集裸题)的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- [java]转:String Date Calendar之间的转换
String Date Calendar之间的转换 String Date Calendar 1.Calendar 转化 String Calendar calendat = Calendar.ge ...
- mysql排序的中文首字母排序和自定义排序
select * FROM organ_new where city_code = 'SZ0755' and organ_type = 'H' and state = '1' ORDER BY FIE ...
- Thymeleaf使用bootstrap及其bootstrap相关插件(二)
接上文http://www.cnblogs.com/conswin/p/7929772.html 接下来bootstrap-datepicker的简单使用. 1.引入添加js 和 css 2.然后是h ...
- URL与URI的含义及区别
1.1 什么是URI? 简单点说:URI就是通用资源标志符,不理解是吧,我第一次听说也是不理解. 进一步说:网络上的一些资源(文档.图片.音频.视频.程序等)都是有一些通用资源标识(Universal ...
- 78. Subsets C++回溯法
本题还是基本的回溯法.就是回溯函数的参数选择上要花点心思! class Solution { public: void backTrack(vector<int> ans, vector& ...
- docker实战系列之搭建rabbitmq
1.搜索镜像[注:因为我这里采用的是阿里云镜像加速器,所以我直接在阿里云中搜索相关镜像路径],点击"详情"查看公网拉取路径 2.拉取镜像 docker pull registry. ...
- postgresql install 报错
install.pm could not copy postgres.exe to ... 错误原因:目标文件夹的父目录不存在
- jvm加载类(更新中)
作为jvm的用户,从使用者角度来看,我们给jvm输入一个class文件,得到了一个Class对象.我们可以猜想下jvm加载类的过程:class文件有规定的格式,jvm去解析class文件流,读magi ...
- js之全局变量与window对象
所有在全局作用域中声明的变量.函数都会变成window对象的属性和方法. 即: var age = 55; 可以通过window.age访问 然而全局变量和与在window对象上定义的属性还是有一点区 ...
- mac+php+nginx+laravel配置启动
首先保证mac安装php,nginx,composer 根据laravel中文文档进行安装 http://laravelacademy.org/post/6665.html 直接指向 composer ...