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.

今天是伊格内修斯的生日。他邀请了很多朋友。现在是晚餐时间。伊格内修斯想知道他至少需要多少张桌子。你必须注意,并非所有的朋友都彼此认识,所有的朋友都不想和陌生人呆在一起。这个问题的一个重要原则是,如果我告诉你A知道B,B知道C,那就意味着A,B,C彼此了解,所以他们可以呆在一张桌子里。例如:如果我告诉你A知道B,B知道C,D知道E,那么A,B,C可以留在一张桌子上,而D,E必须留在另一张桌子上。所以Ignatius至少需要2张牌桌。

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.

输入以表示测试用例数的整数T(1 <= T <= 25)开始。然后是T测试用例。每个测试用例以两个整数N和M(1 <= N,M <= 1000)开始。N表示朋友的数量,朋友被标记从1到N.然后是M行。每行由两个整数A和B(A!= B)组成,这意味着朋友A和朋友B彼此了解。两种情况之间会有空白。

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

对于每个测试用例,只需输出Ignatius至少需要的表格数量。不要打印任何空白。

Sample Input

2
5 3
1 2
2 3
4 5
 
5 1
2 5

Sample Output

2
4
解题思路:题目的意思就是求有多少个最大连通图,采用并查集的方法,将有关系的合并成一个集合,存放每个节点的father数组全部初始化为-1(默认为根节点)。father数组也可以初始化成自身值,因为每个最大的连通图中必有一个节点的根节点为自身值,所以也可以根据这点来查看有多少个最大连通图。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
int t,n,m,a,b,father[maxn];
int find_father(int x){//找根节点
if(father[x]==-)return x;//如果根节点是-1,返回此时的节点编号
else return father[x]=find_father(father[x]);//递归查找根节点
}
void unite(int x,int y){
x=find_father(x);
y=find_father(y);
if(x!=y)father[x]=y;//如果不是同一个连通图,则可以合并
}
int main()
{
cin>>t;
while(t--){
memset(father,-,sizeof(father));//先默认-1为各节点的根节点,跟往常的不一样,因为此题求解是有多少个强连通图
cin>>n>>m;//n表示节点个数,m表示有m种情况
while(m--){
cin>>a>>b;
unite(a,b);//读入时顺便合并
}
int ans=;
for(int i=;i<=n;++i)
if(father[i]==-)++ans;//有多少个根节点是-1,就有多少个最大连通子图
cout<<ans<<endl;
}
return ;
}

题解报告:hdu 1213 How Many Tables的更多相关文章

  1. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  2. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

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

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

  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 How Many Tables (并查集)

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

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

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

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

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

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

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

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

    传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...

随机推荐

  1. 转来的——python webdriver自动化测试初步印象——转来的

    python webdriver自动化测试初步印象 以下示例演示启动firefox,浏览google.com,搜索Cheese,等待搜索结果,然后打印出搜索结果页的标题 from selenium i ...

  2. SQL Server 机考,用T-SQL编写 简单实例

    使用T-SQL实现以下要求: 要求如下: 1,添加数据库:MySchool 2,添加学生基础表:Student 3,添加学生成绩表:ScoreInfo 4,两张表结构分别如下 Student表结构:( ...

  3. 百练2755:神奇的口袋(简单dp)

    描述有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中选择一些 ...

  4. Codeforces Round #240 (Div. 2) D

    , b2, ..., bl (1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n) is called good if each number divides (without a remainde ...

  5. 通过代码学习python之@property,@staticmethod,@classmethod

    URL: https://www.the5fire.com/python-property-staticmethod-classmethod.html #coding=utf-8 class MyCl ...

  6. [codevs 2488]绿豆蛙的归宿(拓扑排序)

    题目:http://dev.codevs.cn/problem/2488/ 分析:这题有个特殊的地方,就是每个边都有可能走到,所以就是每个边的权值*每个边的概率,所以只要求概率,拓扑一下就可以了.

  7. 关于jQuery的append()和prepend()方法的小技巧

    最近工作上有个需求是要求一个自动向上滚动的列表,表有很多行,但只显示一行,每次滚动一行.很简单的一个功能,代码如下 <div class="scroll-area"> ...

  8. 站点过滤器Filter

    --过滤器使用已经非常久了,今天遇到了一个小问题.也就想顺便写一个关于过滤器的博文.记录一下自己使用的感受. 实际上,Filter与Servlet及其相似,差别仅仅是FIlter的doFilter() ...

  9. UNIX网络编程——网络I/O模型

    在学习UNIX网络编程的时候.一開始分不清 同步 和 异步,所以还是总结一下,理清下他们的差别比較好. IO分类 IO依据对IO的调度方式可分为堵塞IO.非堵塞IO.IO复用.信号驱动IO.异步IO. ...

  10. swift手记-6

    // // ViewController.swift // learn // // Created by myhaspl on 16/1/26. // Copyright (c) 2016年 myha ...