hdu 1213 (How Many Tables)(简单的并查集,纯模板)
How Many Tables
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.
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.
2
5 3
1 2
2 3
4 5 5 1
2 5
2
4
#include<stdio.h>
int a[10010];
int f(int x)
{
int r=x;
while(r!=a[r])
r=a[r];
return r;
}
void merge(int x,int y)//此处是int语句的最后加上一个return 0;也是能够的
{
int fx=f(x);
int fy=f(y);
if(fx!=fy)
{
a[fx]=fy;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,n,m,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)//必须从一開始 ,从零開始就WA
{
a[i]=i;//初始化,并作为最后判定的条件。 //将每一个人的根节点初始化自身。最后统计总的根节点的个数
}
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
merge(x,y);//压缩路径函数,降低跟点数量,也就是将认识的人合并
}
int count=0;
for(i=1;i<=n;i++)
{
if(a[i]==i)
count++;
}
printf("%d\n",count);
}
return 0;
}
hdu 1213 (How Many Tables)(简单的并查集,纯模板)的更多相关文章
- 1213 How Many Tables 简单的并查集问题
my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...
- HDU - 1213 dfs求联通块or并查集
思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解
不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
随机推荐
- Android应用程序访问linux驱动第一步:实现并测试Linux驱动
一直都想亲自做一次使用android应用程序访问Linux内核驱动的尝试,但总是没能做到.最近抽出时间,下决心重新尝试一次.尝试的开始当然是先写一个Linux内核驱动了. 我希望写一个简单测驱动程序, ...
- 如何使用awk的比较操作符
对于使用 awk 命令的用户来说,处理一行文本中的数字或者字符串时,使用比较运算符来过滤文本和字符串是十分方便的.下面的部分我们介绍"awk"的比较运算符. awk 中的比较运算符 ...
- Lidgren.Network – an introduction to networking in C# games
Lidgren.Network – an introduction to networking in C# games http://genericgamedev.com/tutorials/lidg ...
- 初始化linux环境
初始化linux环境 1. 新建用户组 addgroup admin //假定为admin用户组 2. 新建用户 useradd -d /home/work -s /bin/bash -m work ...
- iOS 证书管理.p12文件不能导出
iOS证书不能导出p12文件: 首先要确认证书是从你这个电脑上制作生成的! 钥匙串-->我的证书--->右键,就可以导出了!
- vue - src(assets和static)
描述:这里存放一些本地资源(images.css.js). assets:开发服务器(dev Server时引用的临时路径) static:静态资源存放(build Server).
- Oracle DMP 操作笔记之根据DMP逆向推导出导出的表空间名称
最近在带着一群.NET新兵们在开发和升级一套系统,本人虽然工作好几年,但是也是属于啥都懂一点,啥都不会的队伍,碰到新兵更是蛋都碎了,还特别拘谨,为啥新兵们都是基础知识很不错的,看来要好好练习内功了,好 ...
- [Android] SQLite数据库之增删改查基础操作
在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Un ...
- 如何设置outlook实现自动秘密抄送邮件的功能?
很多朋友会发现虽然在家里同步了公司的邮箱可以正常收发邮件,可是每当使用家里的outlook发送相关邮件的时候,在公司的邮箱里找不到相关的发件记录,只能同步收件箱,而不能同步发件箱应该是比较让人困扰的问 ...
- 网站缓存技术(Redis、Memcached、Ehcache)
Redis 是什么? 通常而言目前的数据库分类有几种,包括 SQL/NSQL,,关系数据库,键值数据库等等等. 分类的标准也不一,Redis本质上也是一种键值数据库的,但它在保持键值数据库简单快捷特点 ...