HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables
Problem Description
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
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.
Output
Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=20000;
int pre[maxn],height[maxn];
void init_set(int n){
for (int i = 1; i <= n; i++){
pre[i]=i;
}
memset(height,0,sizeof(height));
} int find_set(int x){
return x==pre[x]?x:pre[x]=find_set(pre[x]);
}
void union_set(int x,int y){
x= find_set(x);
y= find_set(y);
if(x==y)return;
if(height[x]==height[y]){
height[x]=height[x]+1;
pre[y]=x;
}else{
if(height[x]<height[y]) pre[x]=y;
else{
pre[y]=x;
}
}
} int main(){
int T;
cin>>T;
int n,m,a,b;
while (T--){
cin>>n>>m;
init_set(n);
int sum=0;
while (m--){
cin>>a>>b;
union_set(a,b);
}
for (int i = 1; i <=n; i++)
{
if(i==pre[i])sum++;
}
cout<<sum<<endl;
}
}
HDU1213How Many Tables(基础并查集)的更多相关文章
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- HDU1213:How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU1213 How Many Tables (并查集)
题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...
- HDU——1213How Many Tables(并查集按秩合并)
J - How Many Tables Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
随机推荐
- MySQL-11-存储引擎
存储引擎简单介绍 存储引擎:相当于Linux文件系统,只不过比文件系统强大 功能 数据读写 数据安全和一致性 提高性能 热备份 自动故障恢复 高可用方面支持 存储引擎种类 InnoDB MyISAM ...
- MySQL-01-简介以及安装
Mysql简介 什么是数据 数据:文字.图片.视频... 人类认知的数据表现方式 计算机:二进制.16进制的机器语言 基于数据的重要性和复杂性的不同,我们可能有不同的管理方式 哪些数据是适合存储到数据 ...
- Python小白的数学建模课-17.条件最短路径
条件最短路径问题,指带有约束条件.限制条件的最短路径问题.例如: 顶点约束,包括必经点或禁止点的限制: 边的约束,包括必经路段.禁行路段和单向路段:无权路径长度的限制,如要求经过几步或不超过几步到达终 ...
- 题解 P3941 入阵曲
题解 观察数据范围,可以 \(\mathcal O(n^2m^2)\) 暴力计算,而加上特殊性质,则可以骗到 \(75pts\) 正解: 我们发现,在一维情况下,\(\mod k\) 相同的前缀和相减 ...
- docker加速器,设置cdn
添加加速器 vim /etc/docker/daemon.json 添加如下内容 { "registry-mirrors": ["https://registry.doc ...
- WPF 中的形状和基本绘图概述
本主题概述如何使用 Shape 对象绘图. Shape 是一种允许您在屏幕中绘制形状的 UIElement 类型. 由于它们是 UI 元素,因此 Shape 对象可以在 Panel 元素和大多数控件中 ...
- Windows系统搭建Redis集群三种模式(零坑、最新版)
目录 主从复制 修改配置文件 启动各节点 验证 哨兵模式 修改配置文件 启动实例 验证 集群模式 修改配置文件 启动实例 验证 主从复制 新建以下三个目录,用来部署一主二从 redis 的安装在另外一 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 面试必问题:JS防抖与节流
摘要:防抖与节流可谓是面试常见,其实很好理解,下面带你分分钟了解防抖与节流的基本思想与写法~ 本文分享自华为云社区<JS防抖与节流快速了解与应用>,作者:北极光之夜. . 一.速识防抖: ...
- pgsql日期树数值类型指定与介绍
http://www.postgres.cn/docs/9.3/datatype-net-types.html#DATATYPE-INET 文档有详细的pgsql介绍 使用案例: SELECT to ...