点击打开链接

C - How Many Tables

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 1213

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

题目大意:

给你一个数T,代表有几组数据,然后给你两个数m and n,表示有m个同学,

有n种关系,x 和 y 是同学,求最少需要多少桌子,

样例解释:

5 3

1 2

2 3

4 5

1和2是同学, 2和3是同学, 4和5是同学

所以只需要两张桌子,以为有两块(1,2,3)和(4,5);

所以就有两块

解题思路:

纯的并查集,套模板就行,

直接上代码,

ps:我第一次是CE了,因为把下面的代码是AC的,CE是因为rank数组没用着还是咋回事,

我也不太清楚,有知道的可以告诉我哦,我只是把代码的一部分注释了就AC了,

/**
2015 - 09 - 18 晚上
Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 30005;
const double eps = 1e-7;struct node
{int x, y;
} arr[maxn];int rank[maxn], fa[maxn], sum[maxn];
///基本上是模板
void Init(int x)
{for(int i=1; i<=x; i++){fa[i] = i;///rank[i] = 0;sum[i] = 1;}
}int Find(int x)
{if(fa[x] != x)fa[x] = Find(fa[x]);return fa[x];
}void Union(int x, int y)
{int fx = Find(x), fy = Find(y);if(fx == fy)return;/**
    if(rank[fx] > rank[fy])
        fa[fy] = fx, sum[fx] += sum[fy];
    else
    {        fa[fx] = fy;
        if(rank[fx] == rank[fy])
            rank[fy]++;
        sum[fy] += sum[fx];
    }
    **/if(fx != fy)fa[fy] = fa[fx];
}int main()
{int T, m, n, x, y;cin>>T;while(T--){int cnt = 0;cin>>m>>n;Init(m);while(n--){cin>>x>>y;Union(x, y);}for(int i=1; i<=m; i++)if(fa[i] == i)cnt++;cout<<cnt<<endl;}return 0;
}

hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)相关推荐

  1. [kuangbin带你飞]专题五 并查集 题解+总结

    kuangbin带你飞:点击进入新世界 总结: 本人算是初学者中的初学者,欢迎交流~ 并查集的接触过的不多,大概只有普通并查集,带权并查集,种族并查集,传说中的可持续化并查集只是听说过还没有接触,不过 ...

  2. [kuangbin带你飞]专题五 并查集 E - 食物链 (带权并查集)

    E - 食物链 题目链接:https://vjudge.net/contest/66964#problem/E 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C ...

  3. [kuangbin带你飞]专题五 并查集 A - Wireless Network

    A - Wireless Network 题目链接:https://vjudge.net/contest/66964#problem/A 题目: An earthquake takes place i ...

  4. [kuangbin带你飞]专题五查并集

    写了几个查并集得题,成功把自己写晕了 之后写下面得题(写不下去了) **poj-2912 poj 文章目录 1.POJ - 1611(模板题) 2.HDU - 1213(模板题) 3.poj2236( ...

  5. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  6. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  7. “kuangbin带你飞”专题计划——专题十四:数论基础

    写在前面 1.目前还没啥写的.开始时间:2021-05-13(其实博客上看得到该博客创建时间的) 2.上一个专题刷的是网络流(博客总结),属于第一次接触.本来想的是一周特别高效,然后一周略划水,结果是 ...

  8. (2021-07-14~)“kuangbin带你飞”专题计划——专题十三:基础计算几何

    目录 前言 参考博客 自己总结的东西: 难度判断? 题目 1.[TOYS POJ - 2318 ](解决) 2.[Toy Storage POJ - 2398 ](解决) 3.[Segments PO ...

  9. [kuangbin带你飞]专题十二 基础DP1 题解+总结

    kuangbin带你飞:点击进入新世界 总结: 简单dp,最近在做,持续更新. 文章目录 总结: 1.Max Sum Plus Plus 2.Ignatius and the Princess IV ...

最新文章

  1. java中两个Integer类型的值相比较的问题
  2. Oracle创建用户并给用户授权查询指定表或视图的权限
  3. 导入时间过长,请求变成404,导入成功后后台会出现java.io.IOException: 您的主机中的软件中止了一个已建立的连接
  4. ABAP 标准培训教程 BC400 学习笔记之五:ABAP 编程语言的变量,常量和字面量,以及文本符号
  5. pwa 问题_您真的需要PWA吗? 这里有四个问题可以帮助您做出决定。
  6. java io加速器,Java 日期操作
  7. datagridview使用mysql_使用DataGridView进行增删改查,并同步到数据库
  8. C#中,两个事件的叠加,结果会如何?
  9. Gradle发布项目到 maven 之novoda/bintray-release(3)
  10. Splay模板 1.0
  11. vue实现上移下移_vue.js实现组件间的上移下移
  12. linux getopt_long函数,新手疑问:getopt_long()重入问题
  13. 学习笔记(3)-重叠社区发现Copra算法
  14. linux 怎么连接到网络打印机,如何在网络上的Windows,Mac和Linux PC之间共享打印机...
  15. SSL证书不受信任怎么办?重点关注这4点
  16. Windows Server 2008 各版本介绍
  17. 美国亚马逊编辑推荐的一生必读书单100本
  18. UVALive 4850 Installations——思维
  19. 第三节: 水泥混凝土路面构造特点
  20. 2020年阴历二月十一 投资理财读书笔记~漫步华尔街①

热门文章

  1. 非对称加密和证书总结
  2. [Voice Tips 2] IPHONE
  3. CISCO 路由器(2)
  4. MFC窗体控件随窗体变化
  5. Node.js和C++有关的博文链接
  6. Linux系统资源管理 之 硬件信息
  7. MySQL Binlog解析
  8. keepalived+LVS的实现
  9. logic:equal 标签的使用(转)
  10. iPhone开发笔记(1)MPMoviePlayerController的用法和播放时只有声音没有图像的解决办法...