hdu1213 How Many Tables(并查集)
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17946 Accepted Submission(s): 8822
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.
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
pid=1325" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1325
pid=1198" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1198
1102 1162推断几个人是否是朋友。。
。。。
。。。。。。。。。
。
。。。。
。。。
仅仅要我们把这几个人看成几棵树好啦。
。
。我们就数数有几棵树即可。
既然要数有几棵树。那我们怎么区分它们是不是同一棵树呢?就须要推断它们的老祖宗是不是同样。。
。
并查集啦 并查集
看代码,看代码。。
。
先想想思想,再自己动手去做。不要照抄、、、
#include <stdio.h>
#include <string.h>
int fa[1005],n;
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void init()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int main()
{
int ncase,m;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d %d",&n,&m);
init();
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
int x=find(a);
int y=find(b);
if(x!=y)
fa[x]=y;
}
int count=0;
for(int i=1;i<=n;i++)
if(fa[i]==i)
count++;
printf("%d\n",count);
}
return 0;
}
hdu1213 How Many Tables(并查集)的更多相关文章
- hdu1213 How Many Tables 并查集的简单应用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- 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://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU1213最简单的并查集问题
题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...
随机推荐
- hdu 2151
就是一个dp,数组内存的步数, 数组没清空,wa了一次 #include<cstdio> #include<algorithm> #include<cstring> ...
- 0xC0000005;Access Violation(栈区空间很宝贵, linux上栈区空间默认为8M,vc6下默认栈空间大小为1M)
写C/C++程序最怕出现这样的提示了,还好是在调试环境下显示出来的,在非调试状态就直接崩溃退出. 从上述汇编代码发现在取内存地址 eax+38h 的值时出错, 那说明这个地址非法呗, 不能访问, 一般 ...
- Linux下安装intellij idea
1.下载 http://www.jetbrains.com/idea/download/#section=linux 我下载的是不带jdk的版本 2.放入opt目录中 3.解压到usr下面的intel ...
- Java-MyBatis-杂项: MyBatis 中 in 的用法2
ylbtech-Java-MyBatis-杂项: MyBatis 中 in 的用法2 1.返回顶部 1. 一.简介 在SQL语法中如果我们想使用in的话直接可以像如下一样使用: select * fr ...
- 16.unix网络编程一卷 unp.h
unix网络编程 --ubuntu下建立编译环境 1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下 ...
- Redis学习笔记(九) 命令进阶:Pub/Sub(发布/订阅)操作
原文链接:http://doc.redisfans.com/pub_sub/index.html Redis的Pub/Sub模型可以应对工作中的一些简单应用,涉及到复杂应用还是推荐使用诸如Rabbit ...
- element-ui 实现table整列的拖动
演示地址 1. 先动态渲染表头,给每一个表头添加一个class=virtual 的画虚线的类名,同时给每个表头加上鼠标点击.拖动.抬起事件:mousedown->mousemove->mo ...
- Oracle 合并查询
8).合并查询有时在实际应用中,为了合并多个select语句的结果,可以使用集合操作符号union,union all,intersect,minus.多用于数据量比较大的数据局库,运行速度快.1). ...
- js中,实现对键盘按键的监听:
<script> function keyUp(e) { var currKey=0,e=e||event; currKey=e.keyCode||e.which||e.charCode; ...
- Codeforces Round #499 (Div. 2) C.FLY 数学推导_逆推
本题应该是可以使用实数二分的,不过笔者一直未调出来,而且发现了一种更为优美的解法,那就是逆推. 首先,不难猜到在最优解中当飞船回到 111 号节点时油量一定为 000, 这就意味着减少的油量等于减少之 ...