HDU 1856 More is better(进一步优化b)并查集

版权声明:本文为博主原创文章,未经博主允许不得转载。
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang’s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbe~rs A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
*****

题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋友圈,即求最大集合中元素的个数。裸的并查集加个秩数组就行了。
注意当朋友对为0时要特判一下,这里wa了几次,有点不应该,因为题目中写的很清楚0<=n<=1000000。

首先说下cin,cout与scanf,printf的效率问题。
这是cin,cout的
这是printf,scanf的
如果是打比赛的一定要注意这点,不然一不小心就超时了还不知道。 //大佬请忽略。

一般做法
我们发现它的内存很大,因为我们把没有用的空间也用上了
在这里插入图片描述

#include<iostream>
#define mx 10000100
using namespace std;
int pre[mx],num[mx],n,m,i,sum,x,y;
int a[mx];
void init()
{
	sum=0;
	for(i=1;i<=10000100;i++)
	{
		pre[i]=i;
		num[i]=1;
	}
}
int find(int x)
{
	if(x==pre[x])
	return x;
	return pre[x]=find(pre[x]);
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
		num[fy]+=num[fx];
		sum=max(sum,num[fy]);
	}
}
int main()
{
	 while(~scanf("%d",&n))
	 {
	 	if(n==0)
	 	{
	 		printf("1\n");
	 		continue;
		}
	 	init();
	 	for(int i=0;i<n;i++)
	 	{
	 		scanf("%d%d",&x,&y);
	 		join(x,y);
		}	
		printf("%d\n",sum);
	 }
	 return 0;
}

下面是优化的代码
在这里插入图片描述

#include<iostream>
#include<algorithm>
#define mx 10000100
using namespace std;
int pre[mx],num[mx],n,m,i,x,y,a[mx];
int sum=0;
int find(int x)
{
	if(x==pre[x])
	return x;
	return pre[x]=find(pre[x]);
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
		num[fy]+=num[fx];
		sum=max(sum,num[fy]);
	}
}
int main()
{
	 while(~scanf("%d",&n))
	 {
        sum=0;
	 	for(int i=0,j=1;i<n;i++,j=j+2)
	 	{
	 		scanf("%d%d",&x,&y); 
	 		a[j]=x;
	 		a[j+1]=y;
		}	
		for(int i=1;i<=n*2;i++)
		{
			pre[a[i]]=a[i];
			num[a[i]]=1;
		}
		for(int i=1;i<=n*2;i=i+2)
		{
			join(a[i],a[i+1]);
		}
		if(n!=0)
		printf("%d\n",sum);
		else
		printf("%d\n",1);
	 }
	 return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值