解题思路:和畅通工程类似,问最后还剩下几个不连通的区域。

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15086    Accepted Submission(s): 7364

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

#include<stdio.h>
int pre[1005];
int find(int root)
{ return root == pre[root] ? root : pre[root] = find(pre[root]); } void unionroot(int root1,int root2)
{
int x,y;
x=find(root1);
y=find(root2);
if(x!=y);
pre[x]=y; } int main()
{
int i,ncase,n,m,tmp,root1,root2,x,y;
scanf("%d",&ncase);
while(ncase--)
{
tmp=0;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
pre[i]=i; while(m--)
{
scanf("%d %d",&root1,&root2);
x=find(root1);
y=find(root2);
unionroot(x,y);
} for(i=1;i<=n;i++)
{
if(pre[i]==i)
tmp++;
}
printf("%d\n",tmp); }
}

  

HDU 1213 How Many Tables【并查集】的更多相关文章

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

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

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  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://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  5. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  6. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

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

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

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

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

  9. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

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

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

随机推荐

  1. Java中Scanner类的使用

    一个可以解析基本类型和字符串的简单文本扫描器. 例如,以下代码使用户能够从 System.in 中读取一个数: public class ApiScanner { public static void ...

  2. java equals的用法

    equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较.Object类中的equals方法内部使用的就是==比较运算符. package Xuexi; public cla ...

  3. SSH启动失败解决方法

    今天连接linux时居然连不上,报错信息是: 查了一下终于找到了解决办法,只需要一些命令 : cd /etc/ssh sudo chmod 644 ./* sudo chmod 600 ssh_hos ...

  4. OpenGL中着色器,渲染管线,光栅化

    https://www.zhihu.com/question/29163054   光栅(shan一声)化(Rasterize/rasteriztion).这个词儿Adobe官方翻译成栅格化或者像素化 ...

  5. Python笔记6----数组

    1.Python 中的数组 形式: 用list和tuple等数据结构表示数组 一维数组:list=[1,2,3,4] 二维数组:list=[[1,2,3],[4,5,6],[7,8,9]] 用arra ...

  6. asyncio 自动跳出长时间堵塞的 task

    https://www.cnblogs.com/ywhyme/p/10660411.html 的升级版 可以知道当前是卡在哪一个 task 甚至是多少行 import asyncio import o ...

  7. 把SQLAlchemy查询对象转换成字典/json使用(分开)

    注:针对的是查询出来的是单条对象 多个对象的话可以使用for循环遍历查询出来的对象列表,也可以使用下面的方法 1.config.py文件 #!/usr/bin/env python #-*- codi ...

  8. java自带线程池和队列详细讲解,android中适用

    Java线程池使用说明 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后 ...

  9. 自学python 第二天

    1. if基本语句 if 条件: 内部代码块 else: .. . .. . . print(“........”)   if 1 == 1 : print(“a会所”) print(“b会所”) e ...

  10. redis_3 持久化

    快照持久化在本地硬盘保存的数据备份文件: 三个save的意思:数据修改的频率越高,保存的频率也越高,反之. 由于快照持久化是把所有的key和值都备份一遍,这样的操作很消耗资源,为了让系统资源过度的浪费 ...