题目链接:

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): 41546    Accepted Submission(s): 20798

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
 
Author
Ignatius.L
 
Source
 代码如下:
#include<stdio.h>
#include<iostream>
using namespace std;
#define max_v 50005
int pa[max_v];//pa[x] 表示x的父节点
int rk[max_v];//rk[x] 表示以x为根结点的树的高度
int n,ans;
void make_set(int x)
{
pa[x]=x;
rk[x]=;//一开始每个节点的父节点都是自己
}
int find_set(int x)//带路径压缩的查找
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);//找到x的根结点
y=find_set(y);
if(x==y)//根结点相同 同一棵树
return ;
ans--;
if(rk[x]>rk[y])
{
pa[y]=x;
}
else
{
pa[x]=y;
if(rk[x]==rk[y])
rk[y]++;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
if(m+n==)
break;
for(int i=; i<=n; i++)
{
make_set(i);
}
ans=n;
for(int i=; i<m; i++)
{
int x,y;
scanf("%d %d",&x,&y);
union_set(x,y);
}
printf("%d\n",ans);
}
return ;
}

HDU 1213(裸并查集)(无变形)的更多相关文章

  1. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  2. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  3. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  4. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  5. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  6. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  8. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

  9. HDU 1829 分组并查集

    题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...

  10. HDU 1198(并查集)

    题意:给你11个图,每一个都有管道,然后给一张由这11个正方形中的n个组成的图,判断有几条连通的管道: 思路:在大一暑假的时候做过这道题,当时是当暴力来做的,正解是并查集,需要进行一下转换: 转换1: ...

随机推荐

  1. DLL文件

    Dll文件的全称是Dynamic Link Library,中文意思为动态链接库,DLL文件是不可执行文件,其是一个包含由多个程序同时使用的代码和数据的库,动态链接提供了一种方法,使进程可以调用不属于 ...

  2. rocketmq Don't have SubscriptionGroup

    1. 问题描述 rocketmq 生产者发消息正常 mq后台也可以看到发出的消息 但是消费者一直没消费 好像订阅没成功 2. 问题排查 通过上图查看 确实没有检测到订阅者 3. 问题解决 线上环境是 ...

  3. JS算法之八皇后问题(回溯法)

    八皇后这个经典的算法网上有很多种思路,我学习了之后自己实现了一下,现在大概说说我的思路给大家参考一下,也算记录一下,以免以后自己忘了要重新想一遍. 八皇后问题 八皇后问题,是一个古老而著名的问题,是回 ...

  4. MathQuill.js

    MathQuill.js通过html.css.javascript实现数学公式 <p>Type math here: <span id="math-field"& ...

  5. EF Migrations

    Enable-Migrations -EnableAutomaticMigrations dbcontent Add-Migration XXXXX Update-Database -Verbose ...

  6. linux socket中tcp的time_wait的快速回收和重用

    解决方法: 我们可以通过调整内核参数来调整: vi /etc/sysctl.conf 编辑文件,加入以下内容: net.ipv4.tcp_syncookies = net.ipv4.tcp_tw_re ...

  7. 我的MBTI小测试

    今天做了自己的MBTI测试,选了93道题版本的,测试结果是ESFP表演者型——有我在就有笑声.这个测试很有趣,我也觉得很神奇. 一.我的MBTI图形 二.才储分析:我的性格类型倾向为“ ESFP ”( ...

  8. Qt初学——我的第一个UI

    第一次打开Qt的时候,我是一脸懵逼的.没学过c++,里面的程序都看不懂.按照套路,我开始看教程,上手实践.连着搞了3天之后,我开始渐渐明白怎么写UI. 我现在的理解是:UI = 界面设计 + 信号槽响 ...

  9. sqldataAdapter/dataset/datatable的使用

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Loa ...

  10. HTML 的 style 属性

    style 属性用于改变 HTML 元素的样式. This text is in Verdana and red This text is in Times and blue This text is ...