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 Mail::Sender模块发送邮件

    #!/usr/bin/perl -w use strict; use Mail::Sender; ; ){ my $sender = Mail::Sender->new({ smtp => ...

  2. Network: Why 1472B length of ICMP?

    when ping, specifying the length of the packet by: ping localhost -l 32 Actually default is -l 32, s ...

  3. 从头开始学Java【1】

    1:常见的DOS命令 盘符的切换 d:回车 目录的进入 cd javase cd javase\day01\code 目录的回退 cd.. cd\ 清屏 cls 退出 exit 创建目录 md 删除目 ...

  4. 在Eclipse中设置文件的默认打开方式

    在Eclipse中,我们可以设置jsp.xml.js.sql等文件默认打开方式: ①.打开配置选项 ②.找到文件设置 ③.选中我们要设置的文件,默认即可:

  5. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别

    string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回fa ...

  6. JVM保证线程安全

     线程安全 Java内存模型中,程序(进程)拥有一块内存空间,可以被所有的线程共享,即MainMemory(主内存):而每个线程又有一块独立的内存空间,即WorkingMemory(工作内存).普通情 ...

  7. js-知识点

    1.递归,数组选出最大值 var arr = [9,8,55,66,49,68,109,55,33,6,2,1]; var max = arr[0]; function findMax( i ){ i ...

  8. 遍历(一)jquery $().each和$.each()

    原文:http://www.frontopen.com/1394.html 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法. $().each 在dom处理上 ...

  9. 导入excel表格的数据--->到mysql中

    01下载excel类,将Classes文件夹放入ThinkPHP\Extend\Vendor\位置 下载地址 http://phpexcel.codeplex.com/releases/view/26 ...

  10. 向openwrt 源码添加ap143支持

    借鉴地址:http://www.pppei.net/blog/post/536 1.向文件 \target\linux\ar71xx\generic\profiles\atheros.mk 中添加ap ...