How Many Tables(并查集)

探讨如何使用并查集算法解决生日派对中桌位安排的问题,确保互相认识的朋友可以坐在同一张桌子上,通过计算祖先节点来确定最少需要的桌子数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题意:第一个数是几组测试数据,对于每组测试数据来说,第一行n,m;
表示有n 个人,m组关系,接下来的m行a和b,就是说a和b认识;而且,朋友的朋友是朋友。互相认识的人在一张桌子上,问一共需要多少桌子?
并查集的知识,最后看看一共有多少祖先是自己的,就需要多少张桌子。

#include<iostream>
using namespace std;
int pre[1005],t,n,m,cnt;
int find(int x){
	if(x==pre[x])
	return x;
	else return pre[x]=find(pre[x]);
}
void merge(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy) 
	pre[fx]=fy;
}
void init(){
	cnt=0;
	int x,y;
	for(int i=1;i<=n;i++) pre[i]=i;
	for(int i=1;i<=m;i++){
		cin>>x>>y;
		if(find(x)!=find(y)) merge(x,y);
	}
}
int solve(){
	for(int i=1;i<=n;i++){
		if(pre[i]==i) cnt++;
	}
	return cnt;
} 
int main(){
	cin>>t;
	while(t--){
		cin>>n>>m;
		init();
		cout<<solve()<<endl;
	}
	return 0;
}

<think>好的,我现在需要回答用户关于并查集英文名称的问题。首先,用户提到了“disjoint set union”,但不确定是否正确。我需要确认这个术语的正确性。 根据已有的知识,并查集是一种数据结构,用于处理一些不交集合的合并及查询问题。它的英文名称通常有两种说法:Disjoint Set Union (DSU) 或者 Union-Find。这两种名称都是常见的,但具体要看上下文。例如,在算法教材中,可能更常用Union-Find,而DSU则强调其操作。 接下来,我需要检查用户提供的引用内容。在引用[3]中提到:“But how to union sets? You can regard this method as making a tree. We can link two trees into one tree, so the question of how many continuous blocks equals the question of how many trees.” 这里提到了合并集合的方法,类似于树的结构,这与并查集的实现方式一致。因此,引用[3]支持DSU这个术语。 另外,用户提供的其他引用中,引用[1]和[2]涉及集合论和分离性,可能与数据结构名称无关。引用[4]讨论的是Python的数据类型,也不相关。因此,主要依据是引用[3]中的内容。 最后,我需要用中文清晰地回答用户的问题,确认并查集的英文名称是Disjoint Set Union (DSU) 或 Union-Find,并引用正确的文献。</think>并查集的英文名称是**Disjoint Set Union (DSU)**,也常被称为**Union-Find**。这一数据结构主要用于高效管理多个不交集合(即没有公共元素的集合)的合并与查询操作,例如在图算法中判断连通性[^3]。 其核心操作包括: 1. **Find**:确定元素属于哪个集合(通常通过追踪父节点到根节点)。 2. **Union**:将两个集合合并为一个集合,例如通过优化策略(路径压缩、按秩合并)提升效率。 例如,在解决连通块数量问题时,可以用并查集将相连的节点逐步合并到同一个集合中,最终通过统计根节点数量得到结果[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值