过程模板 扫一下一共有几棵树 输出

#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)的更多相关文章

  1. 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解

    不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...

  2. 并查集---体会以及模板&&How Many Tables - HDU 1213

    定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...

  3. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

  4. HDU 1213 How Many Tables(模板——并查集)

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

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

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

  6. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  7. (并查集)How Many Tables -- HDU --1213

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  8. HDU 1213(裸并查集)(无变形)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/ ...

  9. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. 2013年7月份第2周51Aspx源码发布详情

    FineOffice自动化办公(OA)源码  2013-7-12 [ VS2010 ]源码描述:此源码使用fineui开发,作为村居使用的系统,所以命名为fineoffice,其实你在此基础上扩成CR ...

  2. 毕向东day23--java基础-网络总结

    传输层:TCP/UDP   UDP例如:qq聊天,录屏软件,桌面共享     TCP建立链接:三次握手,例如,我叫你一声老王(一次),老王回答说:到.(二次),我对老王说,我知道你到了.(三次握手)! ...

  3. 找不到库文件地址,修改修改方法framework

    直接双击地址行修改

  4. 【Tsinghua OJ】祖玛(Zuma)问题

    描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨 道上并加入原有序列中.一旦有三个或更多同色的珠子变成相 ...

  5. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  6. iOS事件:触摸事件.运动事件.远程控制事件

    iOS中,提供了事件处理:触摸事件,运动事件,远程控制事件.这很大得方便程序猿的工作. 这里先简单做个介绍: // // ViewController.m // demo // // Created ...

  7. PHP CI分页类带多个参数

    通过修改system中的pagination.php,给每个<a>都增加了class="pagination". view页面 <div class=" ...

  8. dedecms 列表页调用自定义字段

    在列表附加字段中添加自己定义的字段 如: lwulr调用:{dede:list addfields="lwurl" channelid="1"}[field:l ...

  9. easyui 入门

    http://www.cnblogs.com/tangge/p/3214496.html 1.页面引用. jquery,easyui,主题easyui.css,图标ico.css,语言zh_CN.js ...

  10. android布局中的divider(目前只知道TableLayout)

    目前在genymotion中设置了之后显示不出来行与行之间的分割线,但是在真机上面是没有问题的 1.使用xml属性添加(3.0以上版本) 设置LinearLayout标签的 android:showD ...