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. Apache的Order Allow,Deny详解

    Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是: O ...

  2. linux中断流程详解

    异常体系比较复杂,但是linux已经准备了很多的函数和框架,但是因为中断是和具体的开发板相关,所以中断需要我们自己来处理一些方面,但是这也是很少的一部分,很多公用的处理函数内核已经实现,linux内核 ...

  3. pyspider介绍及安装

    一.pyspider简介 1.通过python脚本进行结构化信息的提取,follow链接调度抓取控制,实现最大的灵活性 2.通过web化的脚本编写.调试环境.web展现调度状态 3.抓取环模型成熟稳定 ...

  4. jstl表达式引用文件

    <script type="text/javascript" src="<c:url value="/resources/js/common/jqu ...

  5. [Functional Programming Monad] Apply Stateful Computations To Functions (.ap, .liftA2)

    When building our stateful computations, there will come a time when we’ll need to combine two or mo ...

  6. IP地址冲突排查

    普通ARP请求报文(查找别人的IP地址,比如:我需要10.1.1.2的MAC地址,10.1.1.2是别人的IP)广播发送出去,广播域内所有主机都接收到,计算机系统判断ARP请求报文中的目的IP地址字段 ...

  7. TortoiseSVN版本管理软件使用简单说明

    很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器.那么TortoiseSVN就非常适合. 第一步:下载TortoiseSVN,http://tortoisesvn. ...

  8. U3D实现与iOS交互

    原地址:http://502317120.blog.51cto.com/4062300/1077733 在开发中有很多情况下会用到U3D调用iOS中的函数. 例如在U3D中,我们需要调用到一个iOS ...

  9. 修改Jenkins的BUILD_NUMBER

    因为特殊原因需要修改jenkins的BUILD_NUMBER环境变量,现在将修改方法记录一下, Jenkins ->系统管理->脚本命令行 输入以下脚本,运行就Ok Jenkins.ins ...

  10. TensorFlow和深度学习新手教程(TensorFlow and deep learning without a PhD)

    前言 上月导师在组会上交我们用tensorflow写深度学习和卷积神经网络.并把其PPT的參考学习资料给了我们, 这是codelabs上的教程:<TensorFlow and deep lear ...