kuangbin专题五:C - How Many Tables HDU - 1213

C - How Many Tables

HDU - 1213

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.

InputThe 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.

OutputFor 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题意:A 认识 B , B 认识 C , 则 A B C 可以坐在一张桌子上 , 所有相互认识的人组成一个集合 一个集合的人坐一张桌子   问 需要准备多少张桌子 (有多少个集合)。思路:并查集  模板题目。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std ;
#define maxn 5000
int n , m ;
int num1 , num2 ;
int t ;
int father[maxn] ;
bool visit[maxn] ; 

void init(){
    for(int i=1 ; i<=n ; i++){
        father[i] = i ;
    }
    return;
}

int find(int x){
    if(x!=father[x]){
        father[x] = find(father[x]) ;
    }
    return father[x] ;
}

void Union_set(int x , int y){
    int rootx = find(x) ;
    int rooty = find(y) ; 

    if(rootx != rooty){
        father[rooty] = rootx ; 

    }
    return;
}

int check(){
    memset(visit , false , sizeof(visit)) ; 

    int result = 0 ;
    for(int i=1 ; i<=n ; i++){
        visit[find(i)] = true ;
    }
    for(int i=1 ; i<=n ; i++){
        if(visit[i]){

            result ++ ;
        }
    }
    return result ;
}

int main(){

    scanf("%d" , &t) ;
    while(t--){

        scanf("%d %d" , &n , &m) ;
        init() ;
        for(int i=1 ; i<=m ; i++){
            scanf("%d %d" , &num1 , &num2) ;
            Union_set(num1 , num2) ;
        }

        int result = check() ; 

        printf("%d\n" , result) ;
    }
    return 0 ;
}

时间: 2024-10-16 11:37:49

kuangbin专题五:C - How Many Tables HDU - 1213的相关文章

kuangbin 并查集 C - How Many Tables HDU - 1213

How Many Tables  hdu-1213 思路:并查集之后找有几个集 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<stack> #include<map> #include<cstdlib> #incl

kuangbin专题五、并查集

题意:给你1~n的点的坐标,O x,表示x点修好,S x y表示查询x点能否连通y点,连通的条件是dis<d 直接判断最后查询时,是否在同一个集合就可以. ps:注意两点是否在同一集合,必须用find()找,因为fa[]找的,可能不是最终的爸爸. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 int n,d; 6 int x[1005],y[1005]; 7 int vis[1005],f

kuangbin专题五:B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects f

How Many Tables HDU - 1213

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 strang

kuangbin专题 专题九 连通图 HDU 4738 Caocao&#39;s Bridges

题目链接:https://vjudge.net/problem/HDU-4738 题目:tarjan求桥,坑点: 题目说是分岛任务...如果所有岛之间没有完全连通,就不需要执行任务了...答案直接是0... 桥上可能没人,但是,炸弹需要一个人去送,所以至少1个人. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int N

开发指南专题五:JEECG微云快速开发平台代码生成器

开发指南专题五:JEECG微云快速开发平台代码生成器 1.1. Maven开发环境搭建 在搭建jeecg的maven开发环境之前,需要先配置好本机的maven环境,并在eclipse中安装好m2eclipse插件. 1. maven版本的工程目录,代码结构如图311所示. 2. 针对本机开发环境(这里以eclipse为例),调整依赖包和项目属性 首先在工程上右键->properties,在builders选项卡中删除掉不存在或不需要的builders,如图312所示. 然后进入Java Bu

开发指南专题五:JEECG微云高速开发平台代码生成器

开发指南专题五:JEECG微云高速开发平台代码生成器 1.1. Maven开发环境搭建 在搭建jeecg的maven开发环境之前,须要先配置好本机的maven环境,并在eclipse中安装好m2eclipse插件. 1. maven版本号的project文件夹,代码结构如图311所看到的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmdkYWlzY290dA==/font/5a6L5L2T/fontsize/400/fill/I0JBQ

[2016-03-15][HDU][1213][How Many Tables]

时间:2016-03-15 16:19:15 星期二 题目编号:[2016-03-15][HDU][1213][How Many Tables] 题目大意:请朋友吃饭,每个朋友都不喜欢和不认识的人在一桌,给出认识的关系,问至少要多少桌 输入: t组数 每组数据 n m m行 u v 表示u 和 v 认识 输出: 最少 分析:并查集,求集合的数目 #ifdef _WORK_ #include <vector> #include <list> #include <map>

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now