POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
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.
input starts with an integer T(1<=T<=25) which indicate the
number of test cases. Then T test cases follow. Each test case starts
with two integers N and M(1<=N,M<=1000). N indicates the number of
friends, the friends are marked from 1 to N. Then M lines follow. Each
line consists of two integers A and B(A!=B), that means friend A and
friend B know each other. There will be a blank line between two cases.
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<map> using namespace std; int fa[];
bool flag[]; int findf( int x ){
int root = x, pos = x;
while( root != fa[root] ){
root = fa[root];
}
while( pos != root ){
x = fa[pos];
fa[pos] = root;
pos = x;
}
return root;
} int main(){
ios::sync_with_stdio( false ); int t, n, m, a, b, ans;
cin >> t;
while( t-- ){
cin >> n >> m;
for( int i = ; i <= n; i++ ){
fa[i] = i;
}
memset( flag, false, sizeof( flag ) );
ans = ; while( m-- ){
cin >> a >> b;
if( findf( a ) != findf( b ) ){
fa[findf( a )] = b;
}
} for( int i = ; i <= n; i++ ){
int temp = findf( i );
if( !flag[temp] ){
ans++;
flag[temp] = true;
}
} cout << ans << endl;
} return ;
}
POJ-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认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ... 
- poj 2236:Wireless Network(并查集,提高题)
		Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ... 
- poj 1182:食物链(种类并查集,食物链问题)
		食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ... 
- POJ 1456  Supermarket  区间问题并查集||贪心
		F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ... 
- POJ 1182 食物链(种类并查集)
		食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ... 
随机推荐
- 【MySQL】日常小技巧汇总,更新中……
			创建表时修改自增主键,添加 AUTO_INCREMENT=<Number> ,例如: CREATE TABLE `table_name` ( `id` int(11) unsigned N ... 
- 【iOS】设备系统版本
			判断 iOS 系统的版本号,示例代码如下: NSLog(@"version--%d", [[[UIDevice currentDevice] systemVersion] floa ... 
- Mac相关快捷键操作
			拷贝: shift + option + 拖动拖动至目的地 创建快捷方式: option + command + 拖动至目的地 
- poj 1131 Octal Fractions(高精度小数进制转换) Java
			虽然题目那么长其实就是把8进制的浮点数转换成10进制,为了练习Java Biginteger 类 我这里用的是Java,也可以用数组模拟. import java.math.BigDecimal; i ... 
- 深入理解JVM-类加载器深入解析(1)
			类加载 在java代码中,类型的加载,连接与初始化过程都是在程序运行期间完成的 类型:表示的Object本身,并不是指一个对象,也就是class. 运行期间:表示的是一种runtime的概念,在运行期 ... 
- 浏览器输入URL到返回页面的全过程
			[问题描述] 在浏览器输入www.baidu.com,然后,浏览器显示相应的百度页面,这个过程究竟发生了什么呢? [第一步,解析域名,找到主机] 正常情况下,浏览器会缓存DNS一段时间,一般2分钟到3 ... 
- Tomcat源码分析 (二)----- Tomcat整体架构及组件
			前言 Tomcat的前身为Catalina,而Catalina又是一个轻量级的Servlet容器.在美国,catalina是一个很美的小岛.所以Tomcat作者的寓意可能是想把Tomcat设计成一个优 ... 
- Vue系列:为不同页面设置body背景颜色
			由于SPA页面的特性,传统的设置 body 背景色的方法并不通用. 解决方案:利用组件内的路由实现 代码参考如下 
- 使用top查看进程和系统负载信息
			引言 使用top命令,可以查看正在运行的进程和系统负载信息,包括cpu负载.内存使用.各个进程所占系统资源等,top可以以一定频率更新这些统计信息.下面我们来学习top命令的具体使用方法. ... 
- 用xshell链接虚拟机上的linux
			[步骤]一.安装VMware直接点击下一步即可 二.安装linux下载CentOS,在VMware中安装,这个网上有很多教程,这里就不赘述了. 三.配置要想连接上xshell,需要配置IP,将Linu ... 
