HDU1213How Many Tables

Problem Description

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
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

The 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.

Output

For each test case, just output how many tables
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

2
4
 
题意:
  给定T组数据,每组数据给定一个伙伴关系,若A与B是朋友,B与C是朋友,那么A和C也是朋友,具有朋友关系的可以坐在同一张桌子上,假设桌子足够大,问需要多少个桌子。
解题方法:
  这里我们使用并查集来判断各个人是否具有伙伴关系,若有归于同一类,最后统计有多少类即为多少张桌子。
 
代码:

#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(基础并查集)的更多相关文章

  1. 并查集:HDU1213-How Many Tables(并查集最简单的应用)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  2. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  4. HDU1213:How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  6. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  7. HDU1213 How Many Tables (并查集)

    题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...

  8. HDU——1213How Many Tables(并查集按秩合并)

    J - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

随机推荐

  1. 常见的嵌入式linux学习和如何选择ARM芯片问答

    常见的ARM嵌入式学习问答,设计者和学习者最关心的11个问题: 1.          ARM嵌入式是学习硬件好还是学习软件好? 2.          嵌入式软件和硬件,哪一种职位待遇更高?或者说, ...

  2. FSM自动售货机 verilog 实现及 code 细节讲解

    1.题目: 饮料1.5 元, 可投入硬币1 元 0.5 元,输出饮料 零钱 2. 画出状态机. 3.仿真结果:coin=1 --> 0.5 元 coin=2-->1元 4.关键代码分析: ...

  3. 生成二维码项目pom.xml中QRCode依赖报错

    pom.xml报错如下: 解决方法: 1.将QRCode.jar包下载到E盘下 网盘下载地址: 链接:https://pan.baidu.com/s/1sY1NK5ekCkNH0uqraZMnKw 提 ...

  4. IP实验笔记

    代码: 对LSW1: Vlan 10 Interface ethernet 0/0/1 Port link-type access Port default vlan 10 Interface eth ...

  5. linux中文件内核数据结构

    3.文件io 3.1 文件内核数据结构 3.2 复制文件描述符的内核数据结构 3.3 对指定的描述符打印文件标志 #include "apue.h" #include <fc ...

  6. NOIP 模拟 6 模板

    题目 题解 这道题是一道启发式合并的题目,每次合并完重构一下线段树就可以,不用线段树合并. 以操作时间为下标,建立一颗线段树,维护小球的个数与小球的颜色数,最后线段树上二分查找. 我们先不用考虑每个节 ...

  7. http扩展小插件

    支持.net framework4.5.1,.net core2.0及以上 应用层需要引用包Kogel.Net,Nuget上可以下载安装. 或者使用Nuget命令添加包 Install-Package ...

  8. 初识nest.js

    nest的核心概念: Nest的核心概念是提供一种体系结构,它帮助开发人员实现层的最大分离,并在应用程序中增加抽象. 架构预览: 主要有三个核心概念:模块Module,  控制器Controller, ...

  9. 十五:JDBC学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  10. Java基础和常用框架的面试题

    前言 最近学校也催着找工作了,于是刷了一些面试题,学习了几篇大佬优秀的博客,总结了一些自认为重要的知识点:听不少职场前辈说,对于应届毕业生,面试时只要能说到核心重要的点,围绕这个点说一些自己的看法,面 ...