题目描述
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
分析: 一张桌子就是一个集合,合并朋友关系,然后统计集的数量即可。
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int s[maxn];
void init_set(){//初始化
for(int i=1;i<=maxn;++i)s[i]=i;
}
int find_set(int x){//递归查找
return x==s[x]?x:find_set(s[x]);
}
void union_set(int x,int y){//合并
x=find_set(x),y=find_set(y);
if(x!=y)s[x]=s[y];
}
int main(){
int t,n,m,x,y;
scanf("%d",&t);
while(t--){
init_set();
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&x,&y);
union_set(x,y);
}
int ans=0;
for(int i=1;i<=n;++i)
if(s[i]==i)ans++;
printf("%d\n",ans);
}
}
复杂度:在上述程序中,查找与合并的操作,其搜索深度是树的长度,复杂度都是O(n),这样的性能,肯定很难让我们每次都达到满意的效果。要想减少复杂度,就可以优化合并和查找的过程。
合并的优化
在合并元素x和元素y时,先搜到它们的根结点,然后再合并这两个根结点,即把一个根结点的集改成另一个根结点。这两根结点的高度不同,如果把高度更小的集合合并到高度更大的集合上,能减少树的高度。那就在初始化时,用height[i]定义元素i的高度,在合并时更改。
代码如下:
int height[maxn];
void init_set(){
for(int i=1;i<=maxn;++i)
s[i]=i,height[i]=0;
}
void union_set(int x,int y){
x=find_set(x),y=find_set(y);
if(height[x]==height[y]){
height[x]=height[x]+1;
s[y]=x;
}
else{
if(height[x]<height[y])s[x]=y;
else s[y]=x;
}
}
查询的优化——路径压缩
在上面的查询程序中,查询元素i所属的集需要搜索路径找到根结点,返回的结果是根结点,这个搜索路径可能很长。如果在返回的过程中顺便把i所属的集改成根结点,那么下次再搜的时候就能再O(1)的时间内得到结果。
代码如下:
int find_set(int x){
if(x!=s[x])s[x]=find_set(s[x]);
return s[x];
}
上面的代码用递归实现。如果数据规模太大,担心爆栈,可以用下面的非递归代码:
int find_set(int x){
int r=x;
while(s[r]!=r)r=s[r];
int i=x,j;
while(i!=r){
j=s[i];//把路径上元素的集改为根结点
s[i]=r;
i=j;
}
return r;
}
最终优化后的完整代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int s[maxn],height[maxn];
void init_set(){
for(int i=1;i<=maxn;++i)s[i]=i,height[i]=0;
}
int find_set(int x){
if(x!=s[x])s[x]=find_set(s[x]);
return s[x];
}
void union_set(int x,int y){
x=find_set(x),y=find_set(y);
if(height[x]==height[y]){
height[x]++;
s[y]=x;
}
else{
if(height[x]<height[y])s[x]=y;
else s[y]=x;
}
}
int main(){
int t,n,m,x,y;
scanf("%d",&t);
while(t--){
init_set();
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&x,&y);
union_set(x,y);
}
int ans=0;
for(int i=1;i<=n;++i)
if(s[i]==i)ans++;
printf("%d\n",ans);
}
}