题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

How Many Tables

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

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 <iostream>
#include <cstdio>
using namespace std;
int father[]; void set(int n)
{
for (int i=; i<=n; i++)
father[i]=i;
} int find(int a)
{
if (father[a]==a)
return a;
return father[a]=find(father[a]);
} void Union(int x,int y)
{
x=find(x);
y=find(y);
if (x!=y)
father[x]=y;
} int main ()
{
int t,k;
while (cin>>t)
{ while (t--)
{
k=;
int n,m;
cin>>n>>m;
set(n);
while (m--)
{
int s,d;
cin>>s>>d;
Union(s,d);
}
for (int i=; i<=n; i++)
{
if (father[i]==i)
k++;
}
printf ("%d\n",k);
}
}
return ;
}
 

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 1232 畅通工程(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

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

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

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

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

  9. HDU 1863 畅通工程 (并查集)

    原题链接:畅通工程 题目分析:典型的并查集模版题,这里就不详细叙述了.对算法本身不太了解的可以参考这篇文章:并查集算法详解 代码如下: #include <iostream> #inclu ...

  10. Union-Find 并查集算法

    一.动态连通性(Dynamic Connectivity) Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法.动态连通性是计算机图 ...

随机推荐

  1. lucene 学习之基础篇

    一.什么是全文索引 全文检索首先将要查询的目标文档中的词提取出来,组册索引(类似书的目录),通过查询索引达到搜索目标文档的目的,这种先建立索引,再对索引进行搜索的过程就叫全文索引. 从图可以看出做全文 ...

  2. 第14天:逻辑运算符、if、for语句

    今天学习了逻辑运算符.if.for语句基础知识. 一.逻辑运算符 1.&&(与) 一假即假,同真为真2.||(或)一真即真,同假为假3.!(非)切记:参与逻辑运算的,都是布尔值.也就是 ...

  3. 【其他】UTF-8带签名与不带签名

    在 Visual Web Developer 另存为文件时,有编码选项,其中有: Unicode (UTF-8 with signature) - Codepage 65001 Unicode (UT ...

  4. bzoj4501 旅行

    题面: 小C来到了F国,小C想好好地参观F国.F国可以看一个有n个点m条边的有向无环图,小C刚开始站在1号点.假设现在小C站在x号点: 1.点x没有出边,结束旅游. 2.点x有o条出边,小C等概率地选 ...

  5. 【poj2096】Collecting Bugs 期望dp

    题目描述 Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other materia ...

  6. 具体数学数论章-----致敬Kunth

    整除性(divisible): 引入了代表整除性. m\n (m|n) 表示m整除n.注意这里的整除.表示的是n = km(k为整数). 在整除性这里.m必须是个正数.也许你可以描述n 是 m 的k倍 ...

  7. [洛谷P2447][SDOI2010]外星千足虫

    题目大意:有$n$个数,每个数为$0$或$1$,给你其中一些关系,一个关系形如其中几个数的异或和是多少,问最少知道前几个关系就可以得出每个数是什么,并输出每个数 题解:异或方程组,和高斯消元差不多,就 ...

  8. P2672 推销员 优先队列 + 贪心

    ---题面--- 题解: 我会说我想这道普及组题都想了好久么.... 不熟练的普及组选手.jpg 最后随便猜了一个结论居然是对的... 放结论: 假设x = i的最优决策为f[i], 那么f[i + ...

  9. [APIO2015]巴厘岛的雕塑 贪心+DP+特殊数据优化

    写了好久.... 刚刚调了一个小时各种对拍,,,,最后发现是多写了一个等号,,,,内心拒绝 表示一开始看真的是各种懵逼啊 在偷听到某位大佬说的从高位开始贪心后发现可做 首先考虑小数据(因为可以乱搞) ...

  10. C++STL简介

    本文仅仅是个人学习的过程中结合网上博文,对STL的整理,也仅仅是简介.仅为个人学习笔记. 一.STL简介(摘自:晨光(Morning)) STL(Standard Template Library), ...