How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14081    Accepted Submission(s): 6912
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
 
Author
Ignatius.L
 
Source

考察知识点:
并查集。
代码例如以下:
#include<stdio.h>
int a[10010];
int f(int x)
{
int r=x;
while(r!=a[r])
r=a[r];
return r;
}
void merge(int x,int y)//此处是int语句的最后加上一个return 0;也是能够的
{
int fx=f(x);
int fy=f(y);
if(fx!=fy)
{
a[fx]=fy;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,n,m,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)//必须从一開始 ,从零開始就WA
{
a[i]=i;//初始化,并作为最后判定的条件。 //将每一个人的根节点初始化自身。最后统计总的根节点的个数
}
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
merge(x,y);//压缩路径函数,降低跟点数量,也就是将认识的人合并
}
int count=0;
for(i=1;i<=n;i++)
{
if(a[i]==i)
count++;
}
printf("%d\n",count);
}
return 0;
}

hdu 1213 (How Many Tables)(简单的并查集,纯模板)的更多相关文章

  1. 1213 How Many Tables 简单的并查集问题

    my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...

  2. HDU - 1213 dfs求联通块or并查集

    思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

  3. HDU 1213 How Many Tables(并查集,简单)

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

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

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

  5. 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解

    不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...

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

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

  7. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  8. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

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

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

随机推荐

  1. Vue打包项目图片等静态资源的处理

    项目打包,默认是打包在根目录下面的.当然我们可以通过设置,打包到任意子目录中去. 但是,当项目中引入资源的,比如:引入图片资源.js资源.或者字体图标之类的.那么可能在这个中间又会踩坑. 1.在vue ...

  2. 页面优化——js异步载入

    同步载入 在介绍js异步载入之前.我们先来看看什么是js同步载入.我们平时最常使用的就是这样的同步载入形式: <script src="http://XXX.com/script.js ...

  3. Java基础大家必看啊

    写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java语言代码把思路体现出来.   学习 ...

  4. C#秘密武器之表达式树

    一.表达式树入门 Lambda表达式树很复杂,从概念上很难理解清楚,一句话,表达式树是一种数据结构!这里我们通过下面的这个例子来理解一下表达式树,你就能看个大概: lambda表达式树动态创建方法 s ...

  5. Some Web API Url Samples

    URI                               Verb     Description                                               ...

  6. Git学习笔记二--工作区和暂存区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 简单理解: 我们使用mkdir Git在d盘下创建的文件夹,就是工作区,我们编辑readme.txt文件就是在工作区下完成的: gi ...

  7. Ubuntu 下安装adobe reader

    ctrl+alt+t打开终端 wget ftp://ftp.adobe.com/pub/adobe/reader/unix/9.x/9.5.5/enu/AdbeRdr9.5.5-1_i386linux ...

  8. UIImagePickerController 视频录制操作,视频大小,时间长度

    一:使用 iOS 系统 UIImagePickerController 获取视频大小 获取视频长度 - (void)viewDidLoad { [super viewDidLoad]; // Do a ...

  9. UTC时间格式转换

    如‘2018-08-07T14:44:40.000+0800’时间转换为正常时间格式 使用moment库 import moment from 'moment' // 日期格式化 formatTime ...

  10. Errors occurred during the build

    Errors occurred during the build.Errors running builder 'Integrated External Tool Builder' on projec ...