HDOJ并查集题目 HDOJ 1213 HDOJ 1242
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
#include<stdio.h>
#include<stdlib.h>
typedef struct node *ufs;
struct node
{
int parent[];
};
int find(int e,ufs U)
{
while(U->parent[e]!=e)
{
e=U->parent[e];
}
return e;
}
void ufunion(int i,int j,ufs u)
{
u->parent[j]=i;
}
int main()
{
ufs u;
u=(ufs)malloc(sizeof(node));
int n,m,i,t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i=;i<=n;i++)
{
u->parent[i]=i;
}
while(m--)
{
scanf("%d %d",&a,&b);
int t1=find(a,u),t2=find(b,u);
if(t1!=t2)
{
ufunion(t1,t2,u);
}
}
int ans=;
for(i=;i<=n;i++)
{
if(u->parent[i]==i)//父亲节点是他本身,则多开一张桌子
ans++;
}
printf("%d\n",ans);
}
return ;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node *ufs;
struct node
{
int parent[];
};
int find(int e,ufs U)
{
while(U->parent[e]!=e)
{
e=U->parent[e];
}
return e;
}
void ufunion(int i,int j,ufs u)
{
u->parent[j]=i;
}
int main()
{
ufs u;
u=(ufs)malloc(sizeof(node));
int n,m,i,t,a,b;
while(scanf("%d",&n)!=EOF&&n!=)
{
scanf("%d",&m);
for(i=;i<=n;i++)
{
u->parent[i]=i;
}
while(m--)
{
scanf("%d %d",&a,&b);
int t1=find(a,u),t2=find(b,u);
if(t1!=t2)
{
ufunion(t1,t2,u);
}
}
int ans=;
for(i=;i<=n;i++)
{
if(u->parent[i]==i)//父亲节点是他本身,则多开一张桌子
ans++;
}
printf("%d\n",ans-);
}
return ;
}
HDOJ并查集题目 HDOJ 1213 HDOJ 1242的更多相关文章
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...
- 转:并查集总结 例题:hdoj 1232 畅通工程
引述之类的就免了,我们现在做题碰到的并查集基础题目大都是连通城市(或者村庄学校),接下来我们就称每一个城市为一个元素.我们解决此类题目运用的是树结构,每个集合用一棵树表示,而树的节点用于存储集合中的元 ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- HDOJ 1272 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDOJ 3047 带权并查集
解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- 并查集(加权) LA 4487 Exclusive-OR
题目传送门 题意:训练指南P245 分析:首先这道是经典的并查集题目,利用异或的性质.异或性质:x ^ 0 = x -> a ^ a = 0 -> x ^ a ^ a = x,即一个数对某 ...
- 并查集(Disjoint Set)
在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂, ...
随机推荐
- [AaronYang]C#人爱学不学[2]
1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...
- 【web必知必会】—— DOM:四个常用的方法
终于开始复习DOM的知识了,这一阵忙乎论文,基本都没好好看技术的书. 记得去年实习的时候,才开始真正的接触前端,发现原来JS可以使用的如此灵活. 说起DOM就不得不提起javascript的组成了,j ...
- MySQL 索引背后的数据结构及算法原理
本文转载自http://blog.jobbole.com/24006/ 摘要本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引 ...
- JS Jquery去除数组重复元素
js jquery去除数组中的重复元素 第一种:$.unique() 第二种: for(var i = 0,len = totalArray_line.length;i < len;i++) { ...
- strcmp的实现
注意,*str1++和*str2++最好不要写在while判断里,否则需要在return前再*str1-1,和*str2-1. int strcmp(const char *str1,const ch ...
- url的编码问题
JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...
- [原]Golang FileServer
转载请注明出处 今天我们用go来搭建一个文件服务器FileServer,并且我们简单分析一下,它究竟是如何工作的.知其然,并知其所以然! 首先搭建一个最简单的,资源就挂载在服务器的根目录下,并且路由路 ...
- BZOJ-1045 糖果传递 数学+递推
1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...
- CF Gym 100685E Epic Fail of a Genie
传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...