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个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂, ...
随机推荐
- js对象中的回调函数
假设一个页面new多个同类型的对象,且该对象内部含异步请求的回调,很有可能回调函数中的this指向最后一次new的对象. 解决该问题的办法是,异步请求使用$.ajax并制定其context为this, ...
- 利用Ant脚本生成war包的详细步骤
使用ant脚本前的准备 1.下载一个ant安装包.如:apache-ant-1.8.4-bin.zip.解压到E盘. 2.配置环境变量.新增ANT_HOME:E:\apache-ant-1.8.4:P ...
- iOS开发小技巧--利用苹果官方API播放视频(方法已经过时,了解一下)
- 【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之
最后还是去掉异或顺手A了3673,,, 并查集其实就是fa数组,我们只需要维护这个fa数组,用可持久化线段树就行啦 1:判断是否属于同一集合,我加了路径压缩. 2:直接把跟的值指向root[k]的值破 ...
- 淘宝中的UV,PV,IPV
1. UV & PV UV: 店铺各页面的访问人数,一个用户在一天内多次访问店铺被记为一个访客(去重) ; Unique visitors PV: 店铺内所有页面的浏览总量(次数累加); p ...
- 关于IntentFilter的几点注意事项:
http://blog.csdn.net/cnnumen/article/details/8464786 IntentFilter就是用于描述intent的各种属性, 比如action, catego ...
- [Angularjs]ng-class,ng-class-even,ng-class-odd
写在前面 最近在通过angularjs将数据绑定到前端,其中也涉及到很多新的东西,一些效果还是很有必要实现的.在使用中发现ng-class,ng-class-even.ng-class-odd的使用, ...
- C# ServiceStack.Redis 操作对象List
class Car { public Int32 Id { get; set; } public String Name { get; set; } static void Main(string[] ...
- fiddler 挂载 JS文件
有时候,网站 JS 有问题,或者我们想调试JS,就修改了JS,,然后希望它在本地能与网站一起运行,就需要用到 挂载JS了
- Common Pitfalls In Machine Learning Projects
Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...