How Many Tables

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

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

刚学并查集,就以这道题为例题了,并查集没学会怎么学生成树啊,,起初是想做”引水工程“这道省赛题的,结果发现要用最小生成树,,奈何汝家不会,所以发了半天时间看并查集。大一上学期是学过并查集的,但当时什么都没听懂,稀里糊涂的,还是靠自己学会,讲了克鲁斯卡尔与普里姆算法,但克鲁斯卡尔算法实现起来更简单一点;

感觉应该是最基本的并查集了,要不然代码也不会这么短;

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1000+10;
int n,m,a,b,p[N];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);//递归寻找根节点,时间复杂度几乎为常数;
}
void ks()
{
for(int i=1; i<=n; i++)
p[i]=i;//先初始化,每个节点初始的根节点都是本身,当两个节点有联系时就可以合并起来,最终形成树;
while(m--)
{
scanf("%d%d",&a,&b);
int x=find(a),y=find(b);
if(x!=y)
{
p[y]=x;//合并,建议模拟第一组测试数据更好理解;
n--;//一旦可以合并说明可以省下一张桌子(最初需要桌子数就等于总人数);
}
}
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
memset(p,0,sizeof(p));
scanf("%d%d",&n,&m);
ks();
printf("%d\n",n);
}
return 0;
}

详细分解请看小白书201业(算法入门经典),或者百度全面知识,大一菜鸟新人求罩

created at 2016.4.7-21:12

杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~的更多相关文章

  1. 2017杭电ACM集训队单人排位赛 - 6

    2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...

  2. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  3. 杭电acm阶段之理工大版

    想參加全国软件设计大赛C/C++语言组的同学,假设前一篇<C和指针课后练习题总结>没看完的,请先看完而且依照上面的训练做完,然后做以下的训练. 传送门:http://blog.csdn.n ...

  4. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  7. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  8. 杭电acm习题分类

    专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...

  9. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

随机推荐

  1. Jquery 操作HTML5自定义属性data-*

    HTML5自定义属性规范的写法<a data-roleid="12"></a>,也可以直接写<a roleid="12">& ...

  2. springboot 配置Ehcache

    Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法. 第一步:在classpath下引入配置文件ehcache.xml 代码如下: <ehc ...

  3. rhel7使用centos7yum组件

    1)rpm -qa|grep yum --查看已安装的yum组件包 2)rpm -e 包名 --nodeps --卸载包 3)下载安装以下组件包: 使用rpm -ivh yum-* yum-3.4.3 ...

  4. PL/SQL笔记(1)-流程控制,循环,异常,块

    流程控制 1.If,then,else,elsif(不是elseif) ' then null; endif; 2.Case 简单case表达式: 搜索型Case表达式: 3.goto语句 begin ...

  5. jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码

    //获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...

  6. js中json处理总结之JSON.parse

    踩过的坑都将成为路上的风景.队友在cookie中已存以下值: address_info {"address_name":"人民大会堂","...lng ...

  7. ubuntu破解密码方法

    摘要: 开机按住任何键(shift)停住grub菜单,进入advanced option for ubuntu,出现的菜单中,光标移动至…(recovery mode)按E进入编辑,找到ro reco ...

  8. 在同一页面显示多个JavaScript统计图表

    最近我接到一个开发任务,要求就"售后服务客户满意度调查问卷表"里客户填写的反馈答案做一个统计. 问题的例子如下: 您最后一次是何时购买了我们的产品? 服务人员服务态度是否友好.工作 ...

  9. SAP成都研究院姚瑶:软件质量保证工作的变迁

    大家好,我是来自SAP成都研究院Revenue Cloud 团队的质量工程师 , yoyo.很高兴可以和大家分享我个人的工作体会.每个团队都有QE(Quality Engineer), 相信大家对QE ...

  10. scriptPubKey and scriptSig

    First of all two matching scripts are used in two different transactions, one that transfers funds t ...