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. IntelliJ IDEA java开发 WebService

    测试的功能是通过访问solr服务器,获取相关信息,然后以webservice的形式供客户端访问来获取数据.通过查询资料,现在把过程记录如下: 1.环境 IntelliJ IDEA 2017.2.6 J ...

  2. iOS 中 JSON 数据交换格式

         JSON (JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 的详细教程,能够參见 JSON 中国:http://www.json.org.cn/ ...

  3. Discuz常见小问题2-如何在新建的页面上只显示一部分板块

    切换到论坛-版块管理,记住要只显示的板块的gid(比如我的是36)   为某个主导航设置一个单独的php页面(名字自己取)   如果这个页面内容跟首页forum.php完全一样,只是第三行增加了一句话 ...

  4. WCF 之 概述

    WCF全称是Windows Communication Foundation,它是.NET3.0的重要组成部分,用来解决Windows下的一些通信方面的问题.WCF是Microsoft平台上的SOA架 ...

  5. 导出oracle 到 mysql的解决办法

    导出oracle 到 mysql的解决办法 使用sqluldr2 命令如下sqluldr2 USER=weibh/1234@dydb file=c:\1.txt sql=sql.sql   FORMA ...

  6. 算法笔记_076:蓝桥杯练习 结点选择(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值.如果一个点被选择了,那么在树上和它相邻的点都不能被选择.求选出的点的权值和最大是多 ...

  7. C++ STL中Map的按Key排序

    为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序进行存储.这也是作为key的类型必须能够进行<运算比 ...

  8. {...formItemLayout} 标签布局

    {...formItemLayout}是reactjs中属性的写法{...props},formItemLayout标签布局,wrapperCol需要为输入控件设置布局样式时,和label 标签布局, ...

  9. Android Exception 12(has leaked ServiceConnection)

    09-09 15:12:31.154: E/ActivityThread(18855): Activity com..xxx.xx.act.LoadingAct has leaked ServiceC ...

  10. struts2 ValueStack的作用

    Value Stack的作用: 1.       可以作为一个数据中转站 2.       用于在前台-后台之间传递数据,最典型的做法就是struts2标签也ognl表达式的结合.我用得最多的就是数据 ...