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. HTML之基本语法(表单)

    一.表单的基本介绍 表单:就是互联网上用于收集用户信息的一种结构,在HTML当中事先定义好了一种标签来完成此事,标签名称为form,它是一个双标签<form action="" ...

  2. python3从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 方法一:通过栈实现 # -*- coding:utf-8 -*- # class ListNode: # def __ini ...

  3. python_81_标准库_时间模块

    ''' 标准库: 1.time 时间的三种表示方法:a:时间戳(timestamp) b:格式化的时间字符串 c:元组(struct_time)共九个元素 time.struct_time(tm_ye ...

  4. python_80_模块定义导入优化实例

            运行结果 __import__作用: 同import语句同样的功能,但__import__是一个函数,并且只接收字符串作为参数,所以它的作用就可想而知了.其实import语句就是调用这 ...

  5. 单核CPU并发与非并发测试

    多线程运行程序的目的一般是提高程序运行效率并且能够提高硬件的利用率比如多核CPU,但是如果我们只有单核CPU并发运行程序会怎样呢? 我以两个环境作为对比: 环境A(我本机8c) 环境B(我的云服务器1 ...

  6. Mac OS X El Capitan系统完整性保护System Integrity Protection (SIP)

    http://blog.csdn.net/yulimin/article/details/49992031 引言:前段时间经历了XCode编译器代码被注入的事件后,这次 Mac OS X El Cap ...

  7. Mybatis学习记录(2)

    1.mybatis与hibernate不同 Mybatis和hibernate,mybatis不完全是一个ORM框架,因为Mybatis需要程序员自己编写sql语句.mybatis可以通过xml或注解 ...

  8. java在线聊天项目1.3版设计好友列表框功能补充,因只要用户登录就发送一串新列表,导致不同客户端好友列表不同问题

    解决完毕后效果图: 好友列表Vector添加的时候进行判断,如果有相同的则不添加 int flag=0; for (int i = 0; i < names.size(); i++) { if ...

  9. mysql 安装简介

    Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...

  10. linux常用命令(配置查看,定时任务)

    1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...