MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

题目地址:
         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): 2337    Accepted Submission(s): 1033

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

题目分析:

并查集中的超级水题,  只要判断集合的个数就可以了....................

代码如下:

MiYu原创, 转帖请注明 : 转载自 ______________白白の屋

#include <iostream>
using namespace std;
typedef struct {
     int parent;
     int cnt;   
}Tset;

typedef struct treeUFS{
       public:
              treeUFS(int n = 0):N(n+1) { set = new Tset[N];  for ( int i = 0; i != N; ++ i) 
                                                                  set[i].parent = i,set[i].cnt = 1; 
                                        }
              ~treeUFS() { delete [] set; };
              int find ( int x ){ int r = x; while ( set[r].parent != r ) //循环结束,则找到根节点
                                                    r = set[r].parent; int i = x;
                                             //本循环修改查找路径中所有节点
                                             while ( i != r) {   
                                                 int j = set[i].parent; set[i].parent = r; i = j;
                                             } 
                                   return r;
                                }
              void init () { for ( int i = 0; i != N; ++ i) set[i].parent = i,set[i].cnt = 1;  }
              int getSetCount ( int x ){ return set[ find(x) ].cnt; }
              void Merge( int x,int y ){  x = find ( x );  y = find ( y );  
                                           if ( x == y ) return;
                                           if ( set[x].cnt > set[y].cnt ){
                                                set[y].parent = x;
                                                set[x].cnt += set[y].cnt;
                                           }
                                           else{   set[x].parent = y;
                                                   set[y].cnt += set[x].cnt;        
                                               }
                                        }
       private:
              Tset *set;
              int N;         
}treeUFS;

int main ()
{
    int T;
    scanf ( "%d",&T );
    while ( T -- )
    {
           int N,M;
           scanf ( "%d%d",&N,&M );
           treeUFS UFS ( N ); 
           for ( int i = 1; i <= M; ++ i )
           {
                 int a,b;
                 scanf ( "%d%d",&a,&b );
                 UFS.Merge ( a,b ); 
           }
           int nCount = 0;
           for ( int i = 1; i <= N; ++ i )
           {
                if ( UFS.find (i) == i )
                {
                     nCount ++; 
                }
           } 
           printf ( "%d\n",nCount );
    }
    return 0; 
}

转载于:https://www.cnblogs.com/MiYu/archive/2010/08/18/1802673.html

HDOJ 1213 HDU 1213 How Many Tables ACM 1213 IN HDU相关推荐

  1. HDOJ 1874 HDU 1874 畅通工程续 ACM 1874 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址:          http://acm.hdu.edu.cn/showproblem.php?pid=1874 ...

  2. HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...

  3. HDOJ 1253 HDU 1253 胜利大逃亡 ACM 1253 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目描述: ...

  4. HDOJ HDU 2058 The sum problem ACM 2058 IN HDU

    //MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址 :          http://acm.hdu.edu.cn/showproblem.php?pid=2 ...

  5. HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址 : http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目描述: ...

  6. HDOJ 1875 HDU 1875 畅通工程再续 ACM 1875 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址:          http://acm.hdu.edu.cn/showproblem.php?pid=1875 ...

  7. HDOJ 1286 HDU 1286 找新朋友 ACM 1286 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目描述:          http://acm.hdu.edu.cn/showproblem.php?pid=1286 ...

  8. HDOJ HDU 1849 Rabbit and Grass ACM 1849 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址:          http://acm.hdu.edu.cn/showproblem.php?pid=1849 ...

  9. HDOJ HDU 2088 Box of Bricks ACM 2088 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址:          http://acm.hdu.edu.cn/showproblem.php?pid=2088 ...

最新文章

  1. WMI Series :事件预订和处理
  2. 2021年春季学期-信号与系统-第八次作业参考答案-第十小题
  3. 全球首部AI交响变奏曲问世,AI技术应用再拓边界
  4. python下电影_Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法...
  5. Python进阶-函数默认参数,特别是参数传递为空列表
  6. Java核心类库篇6——IO
  7. spark代码中添加logger_Spark RDD中Runtime流程解析
  8. XML SOAP应用简介
  9. 看了扎心:39岁单身程序员入住养老院!养老院:院内平均年龄瞬间年轻了
  10. VS 2005 Beta2 Team Suite Edtion
  11. 【PTAL2-001】紧急救援(Dijkstra+最短路径的条数+最短路径中点权之和的最大值)
  12. java实现电子面单pdf生成_常用快递电子面单批量打印api接口对接demo-JAVA示例
  13. 2019年中国锂电池产业竞争格局
  14. DOS攻击工具——pentmenu
  15. 王垠—写给清华大学的退学申请
  16. 51单片机延时函数不起作用
  17. 高德地图样式主题设置
  18. 京东android面试题2019,2019京东的面试题(牛客)
  19. 机器学习中常见的损失函数_机器学习中最常见的损失函数
  20. 用Matlab绘制相平面图

热门文章

  1. php中的eq的含义,jquery,_jQuery中的eq(0)到底是什么意思??详情请看下面代码!,jquery - phpStudy...
  2. OVS DPDK--介绍(一)
  3. leetcode算法题--K 次串联后最大子数组之和★
  4. C++中vector的capacity和size的区别
  5. 常见算子使用_spark快速入门(二)spark粗略流程简述及常见名词解释
  6. 理解大型分布式网站你应该知道这些概念
  7. ming 贪心 NOIP模拟
  8. 原来这样做运维,就可以不被 KO丨课程推广
  9. 国内优秀Android学习资源汇总全集
  10. Ubuntu安装搜狗输入法