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. MovieReview—Transformers.The.Last.Knight.(变形金刚5:最后的骑士.)

     Gorgeous Effect & Bad Plot   I can only say that the movie's effects are shocking. However, the ...

  2. AutoWidthInput

    import React from 'react'; import PropTypes from 'prop-types'; class AutoWidthInput extends React.Co ...

  3. 关于tomcat的classloader的一点想法

    关于tomcat的classloader相关的帖子网上非常多,我觉得比较好的有: https://www.jianshu.com/p/d90e4430b0b9 https://blog.csdn.ne ...

  4. Sublime +Markdown+OmniMarkupPreviewer 搭建实时预览的markdown编辑器

    浏览器实时预览 <meta http-equiv="refresh" content="0.1"> auto save 的配置 {"aut ...

  5. javaweb基础(11)_cookie的会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  6. lua 分割字符串

    -- 参数:待分割的字符串,分割字符 -- 返回:子串表.(含有空串) function split(str, split_char) local sub_str_tab = {} while tru ...

  7. d3.js--02(data和datum原理)

    原文链接: http://d3.decembercafe.org/pages/lessons/3.html 解析一下data和datum原理: datum():绑定一个数据到选择集上 data():绑 ...

  8. 初探es6

    es6环境 现在的JavaScript 引擎还不能完全支持es6的新语法.新特性.所以要想在页面中直接使用,是会报错的,这时候就需要使用babel将es2015的特性转换为ES5 标准的代码. 1.全 ...

  9. 理解JS闭包的几个小实验

    学了JavaScript有一段时间了,但是对闭包还是不太理解,于是怀着心中的疑问做了几个小实验,终于有点明白了. 首先看一下MDN上的定义:闭包是函数和声明该函数的词法环境的组合. 简单来说,闭包是一 ...

  10. 初涉网络流[EK&dinic]

    主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实 ...