1213 How Many Tables 简单的并查集问题
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 简单的并查集问题的更多相关文章
- 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)(简单的并查集,纯模板)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- The Suspects 简单的并查集
Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not ...
- The Suspects(简单的并查集)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- HDU1213最简单的并查集问题
题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...
- HDU - 1213 dfs求联通块or并查集
思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
随机推荐
- linux的用户,群组和其他用户
1 linux 安全模型:(多任务,多用户的操作系统) 1)使用user和group控制使用者对文件的存储权限. 2)用户使用账户和口令登录linux 3)每个文件都有owner(创建者),owner ...
- 代码块(Block)回调一般阐述
本章教程主要对代码块回调模式进行讲解,已经分析其他回调的各种优缺点和适合的使用场景. 代码块机制 Block变量类型 Block代码封装及调用 Block变量对普通变量作用域的影响 Block回调接口 ...
- <button>与<input type="button"> 的区别
<button> button按钮点击会刷新整个页面 <input type="button"> 不会刷新整个页面 本文为本人用来记录自己做的一些东西,如 ...
- ModelDriven
功能: submit 之后显示结果 1.项目结构 2.web.xml <?xml version="1.0" encoding="UTF-8"?> ...
- seajs简记
参考seajs快速入门 一.前端模块化的价值 解决命名冲突 摆脱文件依赖 性能优化 提高可维护性 seajs.use方法调用通过exports暴露接口通过require引入依赖 二.Sea.js 的常 ...
- DOS下导入导出MySQL备份
导入: 1. cd d:\mysql\bin #cd 到 mysql 的 bin 目录下 2. mysql -u root -p mysql_db_name < d:\mysql\data\ba ...
- IE6/IE7浏览器中"float: right"自动换行的解决方法
在项目中,调试IE6/IE7,发现一个bug,那就是:在同一个div包裹着,几个块级元素并列为一行的情况下,设置了float:right的块级元素会自动换行: 代码如下: <p> < ...
- POJ 2599 A funny game#树形SG(DFS实现)
http://poj.org/problem?id=2599 #include<iostream> #include<cstdio> #include<cstring&g ...
- PHP中使用CURL(五)
curl伪造IP和来源 client.php请求server.php client.php <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_U ...
- NOIP2014-普及组复赛-第一题-珠心算测验
题目描述 Description 珠心算是一种通过在脑中模拟算盘变化来完成快速运算的一种计算技术.珠心算训练,既能够开发智力,又能够为日常生活带来很多便利,因而在很多学校得到普及. 某学校的珠心算老师 ...