题目大意:

  有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从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. BZOJ4318 OSU!(动态规划+概率期望)

    设f[i][0/1]为考虑前i位,第i位为0/1时的期望得分(乘以是0/1的概率).暴力转移显然.前缀和优化即可. 但是这个前缀和精度无法承受,动不动就nan. 考虑增加一位的贡献.若之前后缀1的个数 ...

  2. CF985F Isomorphic Strings

    题目描述 You are given a string s s s of length n n n consisting of lowercase English letters. For two g ...

  3. DPDK 分析

    DPDK 分析 来源 https://www.cnblogs.com/bakari/p/8404650.html 高性能网络技术 随着云计算产业的异军突起,网络技术的不断创新,越来越多的网络设备基础架 ...

  4. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

  5. Spring源码解析-实例化bean对象

    spring加载配置文件,AbstractApplicationContext类中的refresh方法起着重要的作用. @Override public void refresh() throws B ...

  6. Oracle SQL 疑难解析读书笔记(二、汇总和聚合数据)

    2.1 对某字段的值进行汇总 仅仅在两种特殊情况下,Oracle在聚合函数中考虑了NULL值.第一种是在GROUPING功能里,用来检验包含了NULL值的分析函数的结果,是直接由所在的表得来,还是由分 ...

  7. 修改select样式,vue select

    <style> .selectbox{ width: 200px; display: inline-block; overflow-x: hidden; height: 28px; lin ...

  8. kafka+flume+HDFS日志采集项目框架

    1,项目图如下: 2, 实现过程 启动HDFS: sbin/start-dfs.sh 启动zookeeper(三台): bin/zkServer.sh start 启动kafka(三台): root@ ...

  9. VR行业纷纷倒闭:有硬件没内容

    从去年年底开始,VR就成为了一个流行词汇,不仅是巨头公司砸钱布局,众多创业公司也纷纷投入其中.但是,一窝蜂拥入的企业基本都没有成熟的商业模式和赢利模式,只能靠融资供血.在资本寒冬中,大部分的VR企业开 ...

  10. [目前未找到题目]扩展KMP模板

    procedure build_next; begin lena:=length(a);lenb:=length(b); next[]:=lenb;next[]:=lenb-; to lenb- ] ...