(kuangbin带你飞--并查集)How Many Tables

原题目:

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

中文概要:
今天是伊格纳提的生日。他邀请了很多朋友。现在是晚餐时间。伊格纳提想知道他至少需要多少张桌子。你要注意,不是所有的朋友都认识对方,所有的朋友都不想和陌生人在一起。
这个问题的一个重要规则是,如果我告诉你a知道b,b知道c,这意味着a,b,c知道对方,所以他们可以留在一个表里。
例如:如果我告诉你a知道b,b知道c,d知道e,所以a,b,c可以留在一个表中,d,e必须留在另一个表中。所以伊格纳提至少需要两张桌子。
 

#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int f[1001];
void init()
{//先没人一张桌子,给桌子进行编号,桌子的标号就是这个人的序号 
	for(int i=1;i<=n;i++)
	{
		f[i]=i;
	}
 } 
 int getf(int x)
 {//如果两个人不认识 
 	if(f[x]!=x)
 	f[x]=getf(f[x]);
 	return f[x];
 }
int merge(int x,int y)
{
	f[getf(y)]=getf(x);
	return f[getf(y)];
}
int main()
{
	int T,x,y,c;
	scanf("%d",&T);
	while(T--)
	{
		c=0;
		scanf("%d %d",&n,&m);//n个人,m个关系 
		init();
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&x,&y);//x和y有关系 
			merge(x,y);
		}
		for(int i=1;i<=n;i++)
		{
			if(f[i]==i)
			c++;
		}
		printf("%d\n",c);
	}
	return 0;
}

套用白书模板都是内存超限,不知道是为什么。。。。

这里面合并ab的集合不知道为什么要这样写。。。

并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin」专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值