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. Boostrap(4)

    1.按钮 <!doctype html> <html> <head> <meta charset="utf-8"> <titl ...

  2. 报课系统APP

    031302307黄丰润 031302343张晓燕 #NABCD模型分析 合理分析需求有助于说服客户,所以我们有如下分析 N(need)--客户需要什么 负责人需要将选课信息和选课表格一起发送给所负责 ...

  3. 【codevs 1296】营业额统计 水~~

    今天下午先写一个Splay水题来复习一下Splay模板.是不是有点太水了做这种水题我有点良心不安. 可笑的是一开始我竟然WA了一组,看来是我低估水题的数据范围了,我是空节点直接返回inf或-inf,明 ...

  4. DOM0,DOM2,DOM3事件,事件基础知识入门

    事件是javascript和HTML交互基础, 任何文档或者浏览器窗口发生的交互, 都要通过绑定事件进行交互; 事件有DOM0, DOM2和DOM3的区分(别问我怎么少了一个DOM1, 也没找到DOM ...

  5. xml_MathML的基本知识点__这东西要自己实践最好

    1 : <mi> 一般的字符串 2: <mo> 操作字符串 <mo> ( </mo> <mo>∑</mo> 3:<mn&g ...

  6. Java基础-常量,变量,成员变量,局部变量

    在java中,数据是以常量和变量两种方法形式进行存储和表示的(实际上,所有程序的数据都是这两种形式). 变量 变量代表程序的状态.程序通过改变变量的值来改变整个程序的状态,或者说得更大一些,也就是实现 ...

  7. 设计模式原来如此-代理模式(Proxy Pattern)

    代理模式(Proxy Pattern)是一个使用率非常高的模式,其定义如下:为其他对象提供一种代理以控制对这个对象的访问. 在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端 ...

  8. POJ2485Highways(prime 水题)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26516   Accepted: 12136 Descri ...

  9. P1049送给圣诞夜的礼品(矩阵十大问题之四)

    https://vijos.org/p/1049 P1049送给圣诞夜的礼品 Accepted 标签:组合数学送给圣诞夜的礼物[显示标签]     返回代码界面 | 关闭   Pascal Pasca ...

  10. ExtJS入门教程02,form也可以很优雅

    在上一篇<Extjs window 入门>中,我们已经看到了如何将一个form组件放到window中,今天我们来看看form的一些优雅的工作方式. 使用fieldDefaults,优雅的设 ...