题目大意:

  有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了)。问需要多少桌子。

思路:

  并查集的基础题目,pre数组存的是父节点的值,root数组代表是否为根节点。最后统计根节点的数量就可以了~~~~~

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + ;
int pre[MAXN];
bool root[MAXN]; int Find(int x)
{
int r = x;
while(pre[r] != r)
{
r = pre[r];
}
int i = x,j;
while(pre[i] != r) //路径压缩
{
j = i;
i = pre[i];
pre[j] = r;
}
return r;
} void mix(int a,int b)
{
int x = Find(a);
int y = Find(b);
if(x > y)
{
pre[x] = y;
}
if(x < y)
{
pre[y] = x;
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int M,N;
for(int i = ; i <= MAXN; i++)
{
pre[i] = i;
root[i] = false;
}
scanf("%d%d",&M,&N);
while(N--)
{
int a,b;
scanf("%d%d",&a,&b);
mix(a,b);
}
for(int i = ; i <= M; i++)
{
if(pre[i] == i)
root[i] = true;
}
int ans = ;
for(int i = ; i <= M; i++)
{
if(root[i]) ans ++;
}
printf("%d\n",ans);
}
return ;
}

HDU1213 How Many Tables (并查集)的更多相关文章

  1. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  3. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  4. C - How Many Tables 并查集

    Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...

  5. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

  6. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  7. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  8. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  9. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  10. HDU1213最简单的并查集问题

    题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...

随机推荐

  1. [洛谷P4001][BJOI2006]狼抓兔子

    题目大意:给你一个n*m的网格图,有三种边,横的,纵的和斜的,要你求出它的最小割 题解:网络流 卡点:1.无向图,反向弧容量应和正向弧相同 C++ Code: #include<cstdio&g ...

  2. cf 442 D. Olya and Energy Drinks

    cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...

  3. 【BZOJ4325】NOIP2015 斗地主 搜索+贪心

    这个东西考试的时候一眼以为状压就压炸了考试又了一下午.....最后我打出来发现后几个点10min都过不去,我大概算了一下,可能是吧.......最后一脸懵逼的我去怂了正解,我们发现只要确定了顺子就可以 ...

  4. poj 2104 (划分树模板)

    Description You are working for Macrohard company in data structures department. After failing your ...

  5. eclipse中的debug按钮组突然找不到了,找回方法

  6. Join/remove server into/from windows domain PS script

    Join server into windows domain PS script $username = "ad-domain\admin" $Password = " ...

  7. vsftpd主动模式和被动模式的区别

    何为主动模式,何为被动模式 1.ftp采用两个端口控制: 20端口用于数据传输. 21端口用于控制,或指建立TCP连接. 2.主动方式连接过程: [注意]:C表示客户端 S表示服务器端 S端要开启20 ...

  8. css中文本超出部分省略号代替

    p{ width: 100px; //设置p标签宽度 white-space: nowrap; //文本超出P标签宽度不换行,而是溢出 overflow: hidden; //文本超出P标签,超出部分 ...

  9. 最适合初学者学习的idea教程

    https://github.com/judasn/IntelliJ-IDEA-Tutorial

  10. xampp命令

    XAMPP命令安装 XAMPPtar xvfz xampp-linux-1.6.4.tar.gz -C /opt启动 XAMPP/opt/lampp/lampp start停止 XAMPP/opt/l ...