Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
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.
 
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
 
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 
Sample Input
2
5 3
1 2
2 3
4 5
 
5 1
2 5
 
Sample Output
2 4
 
并查集水题,完全不用用到并查集的优化算法,父亲节点若为本身,则自开一桌;
 
附AC代码:
 #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 ;
}
 HDOJ 1232类似上题
Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 注意:两个城市之间可以有多条道路相通,也就是说 3 3 1 2 1 2 2 1 这种输入也是合法的 当N为0时,输入结束,该用例不被处理。
 
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
 
Sample Input
4 2
1 3
4 3
 
3 3
1 2
1 3
2 3
 
5 2
1 2
3 5
 
999 0
 
0
 
Sample Output
1
0
2
998
 
附AC代码:
 #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的更多相关文章

  1. 并查集(涂色问题) HDOJ 4056 Draw a Mess

    题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...

  2. 转:并查集总结 例题:hdoj 1232 畅通工程

    引述之类的就免了,我们现在做题碰到的并查集基础题目大都是连通城市(或者村庄学校),接下来我们就称每一个城市为一个元素.我们解决此类题目运用的是树结构,每个集合用一棵树表示,而树的节点用于存储集合中的元 ...

  3. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  4. HDOJ 1272 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. HDOJ 3047 带权并查集

    解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...

  6. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  7. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  8. 并查集(加权) LA 4487 Exclusive-OR

    题目传送门 题意:训练指南P245 分析:首先这道是经典的并查集题目,利用异或的性质.异或性质:x ^ 0 = x -> a ^ a = 0 -> x ^ a ^ a = x,即一个数对某 ...

  9. 并查集(Disjoint Set)

    在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂, ...

随机推荐

  1. [AaronYang]C#人爱学不学[2]

    1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...

  2. 【web必知必会】—— DOM:四个常用的方法

    终于开始复习DOM的知识了,这一阵忙乎论文,基本都没好好看技术的书. 记得去年实习的时候,才开始真正的接触前端,发现原来JS可以使用的如此灵活. 说起DOM就不得不提起javascript的组成了,j ...

  3. MySQL 索引背后的数据结构及算法原理

    本文转载自http://blog.jobbole.com/24006/ 摘要本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引 ...

  4. JS Jquery去除数组重复元素

    js jquery去除数组中的重复元素 第一种:$.unique() 第二种: for(var i = 0,len = totalArray_line.length;i < len;i++) { ...

  5. strcmp的实现

    注意,*str1++和*str2++最好不要写在while判断里,否则需要在return前再*str1-1,和*str2-1. int strcmp(const char *str1,const ch ...

  6. url的编码问题

    JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...

  7. [原]Golang FileServer

    转载请注明出处 今天我们用go来搭建一个文件服务器FileServer,并且我们简单分析一下,它究竟是如何工作的.知其然,并知其所以然! 首先搭建一个最简单的,资源就挂载在服务器的根目录下,并且路由路 ...

  8. BZOJ-1045 糖果传递 数学+递推

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...

  9. 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 ...

  10. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...