HDU - 1213 How Many Tables【并查集】

How Many Tables

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49429 Accepted Submission(s): 24579

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

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213

简述:n表示有多少个人,m表示有几组朋友。如果a跟b在一组,表示他们相互认识可以在一桌,如果b跟c也认识,则a、b、c可以在一桌。

分析:用一个数组来储存每个人对应的号码,如果两个人相互认识,则修改其中一个人的数组内容,使之与父节点相同,当这个人同时被第三个人认识时,再次修改第三个人的数组内容,使之与父节点相同。

说明:一开始没有写find函数,跟题目给出的测试结果是相同的,结果WA,后来认真想了想,当出现了另一种比如a跟b认识,a跟c认识的时候就会出错,才有点明白find函数回溯的作用。

AC代码如下:

#include <iostream>
#include <cstring>
using namespace std;
int a[1010];
int find(int d)
{
	if (d != a[d]) a[d] = find(a[d]); //如果数组对应的值已经被修改了,那么回溯到它的父节点。
	return a[d];
}
int hebing(int q, int w)
{
	return a[q] = w;
}
int main()
{
	int t, k;
	cin >> t;
	while (t--)
	{
		k = 0;
		int n, m, i, q, w;
		cin >> n >> m;
		for (i = 1; i <= n; i++)
		{
			a[i] = i;
		}
		for (i = 0; i < m; i++)
		{
			cin >> q >> w;
			q = find(q);
			w = find(w);
			if (q != w) hebing(q, w);
		}
		for (i = 1; i <= n; i++)
		{
			if (a[i] == i) k++;
		}
		cout << k << endl;
	}
}
#include <iostream>
using namespace std;
int map[1010];
int find(int a) {	
	if (map[a] != a) map[a] = find(map[a]);	//这里修改值
	return map[a];
}
int main()
{
	int T, n, m, a, b, i, q, w, k;
	cin >> T;
	while (T--) {
		k = 0;
		cin >> n >> m;
		for (i = 1; i <= n; i++)
			map[i] = i;
		while (m--) {
			cin >> a >> b;
			q = find(a);
			w = find(b);
			if (q != w) map[q] = w;
		}
		for (i = 1; i <= n; i++)
			if (map[i] == i) k++;
		cout << k << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值