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. Redis14--jedis实现主从模式。

    redis的主从模式之前提到过,这里我们使用redis来实现主从模式.首先在VMware虚拟机中的Linux中打开两个终端,一个是用户jack,一个是newuser:然后我们jack作为主机,redi ...

  2. winform上传文件

    //上传图片 文件 public int addUpPic( String strProCode,String strFileName,String strUpType) { //strFileNam ...

  3. HDU:3368-Reversi(暴力枚举)

    Reversi Time Limit: 5000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

     spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...

  5. Javaweb 第5天 mysql 数据库课程

    MySQL数据库课程 两日大纲 ● 数据库的概念.MySQL快速入门.SQL语言简介 ● 数据库操作.表操作.数据记录操作.数据类型和约束 ● 查询 ● 多表关系.多表连接查询 ● 视图 ● 数据备份 ...

  6. 编写高质量iOS代码的52个有效方法2-1

    一.变量的定义位置(用{}声明示例变量或者用@property属性声明实例变量) 1.用{}声明示例变量: 此方法生命的实例变量,编译器在编译时,会自动计算其偏移量(表示该变量距离存放对象的内存区域的 ...

  7. HDU 1054 Strategic Game(无向二分图的最大匹配)

    ( ̄▽ ̄)" //凡无向图,求匹配时都要除以2 #include<iostream> #include<cstdio> #include<algorithm&g ...

  8. navicat 连接远程mysql

    01putty中session---远程地址(程序访问的域名) ,端口22--load加载进来 02SSH--Auth-Tunnels(隧道)-- putty端口映射SSH--Auth-Tunnels ...

  9. SSL读书笔记

    摘要: 第一次写博客,为读书笔记,参考书目如下: <HTTP权威指南> <图解HTTP> <大型分布式网站架构设计与实践> 作者:陈康贤 一. HTTP+SSL=H ...

  10. C# 语言规范_版本5.0 (第0章 目录)

    C# 语言规范 版本5.0 注意 © 1999-2012 Microsoft Corporation.保留所有权利. Microsoft.Windows.Visual Basic.Visual C# ...