J - How Many Tables

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

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<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
const int N=1010;
int pre[N];
int ran[N];
void init()
{
for (int i=0; i<N; i++)
{
pre[i]=i;
ran[i]=1;
}
}
int find(int n)
{
if(n!=pre[n])
return pre[n]=find(pre[n]);
return pre[n];
}
inline void joint(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa!=fb)
{
if(ran[fa]>ran[fb])
{
pre[fb]=fa;
ran[fa]+=ran[fb];
ran[fb]=0;
}
else
{
pre[fa]=fb;
ran[fb]+=ran[fa];
ran[fa]=0;
}
}
}
int main(void)
{
int tcase,i,j,x,y,n,m;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d%d",&n,&m);
for (i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
joint(x,y);
}
int ans=0;
for (i=1; i<=n; i++)
{
if(ran[i]!=0)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

HDU——1213How Many Tables(并查集按秩合并)的更多相关文章

  1. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...

  2. 【bzoj4668】冷战 并查集按秩合并+朴素LCA

    题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...

  3. Dash Speed【好题,分治,并查集按秩合并】

    Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...

  4. HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. 【BZOJ-4668】冷战 并查集 + 按秩合并 + 乱搞

    4668: 冷战 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 37  Solved: 24[Submit][Status][Discuss] Des ...

  6. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并

    原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...

  7. bzoj4668: 冷战 并查集按秩合并

    题目链接 bzoj4668: 冷战 题解 按秩合并并查集,每次增长都是小集合倍数的两倍以上,层数不超过logn 查询路径最大值 LCT同解 代码 #include<bits/stdc++.h&g ...

  8. 石头剪刀布(2019Wannafly winter camp day3 i) 带权并查集+按秩合并 好题

    题目传送门 思路: 按照题意描述,所有y挑战x的关系最后会形成一棵树的结构,n个人的总方案数是 3n 种,假设一个人被挑战(主场作战)a次,挑战别人(客场)b次,那么这个人存活到最后的方案数就是3n* ...

  9. bzoj 4668 冷战 —— 并查集按秩合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4668 按秩合并维护并查集的树结构,然后暴力找路径上的最大边权即可. 代码如下: #inclu ...

随机推荐

  1. MySql5.7主从配置

    记录 环境:ubuntu16.04,mysql5.7 主机:192.168.1.240,192.168.1.241:241为Salve 1.安装mysql sudo apt-get install m ...

  2. Android(java)学习笔记112:Activity中的onCreate()方法分析

    1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...

  3. Acronis.Disk.Director磁盘分区管理

    Acronis.Disk.Director分为for 专业版和服务器版的,我在生产环境中调整Windows2003跳板机使用的是Acronis.Disk.Director Server 10.0.20 ...

  4. ubuntu下安装eclipse<转>

    转载自http://my.oschina.net/u/1407116/blog/227084      http://my.oschina.net/u/1407116/blog/227087 一 JD ...

  5. 【kmp】bzoj3620: 似乎在梦中见过的样子

    考察kmp理解题 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中 ...

  6. ubuntu系统普通用户密码忘记之重置

    当我们在使用ubuntu系统忘记普通用户登录密码的时候,会被系统在登录界面拒之门外而不得入,这时候只好需要我们去重新设置密码,具体做法如下: 系统重启,在GRUB模式下选择Advanced Optio ...

  7. python爬虫基础13-selenium大全7/8-异常

    Selenium笔记(7)异常 本文集链接:https://www.jianshu.com/nb/25338984 完整文档 Exceptions that may happen in all the ...

  8. leetcode-15-basic-string

    58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...

  9. LeetCode(201) Bitwise AND of Numbers Range

    题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...

  10. VR开发的烦恼——范围限制

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52230865 作者:car ...