kuangbin_UnionFind C (HDU 1213)
过程模板 扫一下一共有几棵树 输出
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define MAX 1010
using namespace std;
int father[MAX],table[MAX];
int Findfather(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
void Union(int x,int y)
{
x=Findfather(x);
y=Findfather(y);
father[x]=y;
}
int main()
{
int t,n,m,count;
scanf("%d",&t);
while(t--)
{
count=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
father[i]=i;
table[i]=;
}
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
Union(a,b);
}
for(int i=;i<=n;i++)
{
int tmp=Findfather(i);
bool same=false;
for(int j=;j<=count;j++)
if(tmp==table[j])
{
same=true;
break;
}
if(!same){
table[++count]=tmp;
}
}
printf("%d\n",count);
}
return ;
}
kuangbin_UnionFind C (HDU 1213)的更多相关文章
- 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解
不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...
- 并查集---体会以及模板&&How Many Tables - HDU 1213
定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...
- 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 Problem Description Today is Ignatius' birthday ...
- 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 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- (并查集)How Many Tables -- HDU --1213
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1213(裸并查集)(无变形)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/ ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- IntelliJ IDEA 12.0
User name:JavaDeveloper Serial number:92547-KY2BB-QZ0S1-PEZCV-HUT8Q-6RYY4
- python解无忧公主数学题107.py
python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...
- hadoop启动之后出现错误:Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s);
INFO ipc.Client: Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s); re ...
- 强引用strong和弱引用weak的定义
1.强引用表示从属关系,引用对象拥有被引用的对象.弱引用则暗示引用对象不拥有被引用的对象.一个对象的寿命是由它被强引用多少次来决定的.只要对象还存在强引用,就不会释放该对象. 注意:但是对象之间的引用 ...
- IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- C++之创建对象时的new与不new
C++在创建对象的时候可以采用两种方式:(例如类名为Test) Test test 或者 Test* pTest = new Test(). 这两种方法都可以实例化一个对象,但是这两种方法有很大的区 ...
- oracle第一招之神马都是浮云
oracle: 一款关系型(二维表)数据库,可以用来存储海量数据.在大数据量并发检索的情况下,性能要高于其他的同类数据库产品.一般运行环境是Linux和Unix操作系统上! 目前最流行的商业数据库,主 ...
- 推荐mysql优化的21条经验
1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这 1. 为查询缓存优 ...
- javascript js写特效日历
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- test Windows Live Writer
1, 下载Live Writer http://windows.microsoft.com/zh-cn/windows-live/essentials-other#essentials=overvie ...