my code:

#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
int find(int num,int A []){
while(num!=A[num])//{
num = A[num];
return num;
}//

bool follow(int a,int b,int A[]){
int i = find(a,A);
int j = find(b,A);
//cout<<"zxd";
//cout<<i<<" "<<j<<" ";
if(i == j)return false;
//cout<<"zxd";
A[i] = j;
return true;
}

int main()
{
int t;
scanf("%d",&t);
while(t--){
//下面的两个变量是人数和关系两个变量
int m,n;
scanf("%d%d",&m,&n);
//在这里还是先创建一个动态数组,来保存返回值
int *A = new int [m+1];
for(int i = 1;i <= m;i++)A[i]=i;//for(int i = 1;i <= m;i++)cout<<A[i];

int a,b,count = 0;
for(int i = 0; i < n; i++){
scanf("%d%d",&a,&b);
// cout<<"循环中ing"<<a<<b<<endl;
if(follow(a,b,A))count++;
// cout<<"count++;"<<count<<endl;
}

printf("%d\n",m - count);
delete [] A;
}
return 0;
}

//在这里有两个关键的函数,也就是PPT中的精华部分

int find(int a)

//查找根结点

{

while(a != fa[a])

//这里要说明的一点是fa[a]是用来判断自己的上一个节点是什么,最后一个东西就是默认根节点的根节点是自己本身

a = fa[a];

return a;

}

int find(int a)

{

return fa[a] == a?a:fa[a] = find(fa[a]);

//在找到根节点的同时压缩了路径 ,将保存父节点的数组更改为根节点

}

union(u,v)

{

fa_u = find(u);

fa_v = find(v);

if(fa_u != fa_v)

fa[fa_u] = fa_v;

}

1213 How Many Tables 简单的并查集问题的更多相关文章

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

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

  2. hdu 1213 (How Many Tables)(简单的并查集,纯模板)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. The Suspects 简单的并查集

    Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not ...

  4. The Suspects(简单的并查集)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  5. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  6. HDU1213How Many Tables(基础并查集)

    HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...

  7. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  8. HDU1213最简单的并查集问题

    题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...

  9. HDU - 1213 dfs求联通块or并查集

    思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

随机推荐

  1. 用Perl做个简单”下载者病毒”

    一直学的perl, 有时perl不理解时就用python写一下,这样或许perl就理解了 这里参照python写法,做了个perl的版本,当然,是为了学习用,这个下载者病毒有点简单过头了 backdo ...

  2. jquery学习笔记3 jq遍历

    遍历方式:向上(父级元素)  向下(子元素)    水平(同胞元素) 一.向上遍历 parent()      向上一级   放回被选元素的直接父元素 parents()          返回被选元 ...

  3. UITextField(一)监听输入内容的变化(开发笔记)

    //添加事件UIControlEventEditingChanged [textField addTarget:self action:@selector(textFieldDidChange:) f ...

  4. mybatis 主键UUID生成策略

    <insert id="insert" parameterType="com.lsfwpt.lawmis.po.SysUser"> <sele ...

  5. CSU 1333 Funny Car Racing

    最短路问题稍微复杂了一点,松弛的时候多判断一些条件就可以了.第一次用SPFA写最短路. #include<cstdio> #include<cmath> #include< ...

  6. javascript中关于继承的理解

    首先,你要理解在javascript中,每当一个新函数创建时,都会生成一个prototype属性,我们管它叫做原型对象.看一个例子: function foo(){ this.name='qiangq ...

  7. Commix命令注入漏洞利用

    介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏 ...

  8. js--事件对象的理解4

    阻止默认行为:1.实例-简易修改右键菜单<script>document.oncontextmenu=function (ev){ var oEvent=ev||event; var oU ...

  9. validform 怎么验证小数。

    <input type="text" class="input-text" value="{$info.score}" placeho ...

  10. Sass入门:第四章

    1.加法 加法运算是Sass中运算的一种,在变量或属性中都可以做加法运算.如: .box { width: 20px + 8in; } 编译出来的CSS: .box { width: 788px; } ...