HDU1856 H - More is better(并查集)

本文深入探讨了并查集算法的应用场景,特别是在处理大规模社交网络关系中的朋友对问题时,如何通过并查集来确定最大可能保留的个体数量。文章详细介绍了算法实现,包括初始化、查找和合并操作,并通过一个具体示例展示了算法的有效性和效率。

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

Description

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 numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

当N为0时输入结束。
Output

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

Sample Input

4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8

Sample Output

4

2

这道题是一个并查集的题目,只不过不是问有多少个集合,而是找集合中的元素个数,只需要合并的时候将合并的子节点数目加到父节点上就好了。

#include<bits/stdc++.h>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

int f[10000005],vis[10000005];



int fid(int x)
{
	if(f[x]==x) return x;
	else return f[x]=fid(f[x]);
}

void joint(int x,int y)
{
	int fx=fid(x);
	int fy=fid(y);
	if(fx!=fy)
	{
		f[fx]=fy;
		vis[fy]+=vis[fx];//将子节点上的数目加到父节点上
	}
}
int main()
{
	int n,m,x,y,t;
	while(scanf("%d",&n)!=EOF)

	{


		int sum=1,t=1,flag=0;
		int cnt=0,x,y;

		for(int i=1;i<=10000005;i++) f[i]=i;
		for(int i=1;i<=10000005;i++) vis[i]=1;// 初始为每个都为1
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);

			//show2(fid(x),fid(y));
			t=max(x,t);
			t=max(y,t);
			joint(x,y);

		}
		//cout<<t<<endl;

		for(int i=1;i<=t;i++)
		{

			cnt=max(cnt,vis[i]);

		}
		printf("%d\n",cnt);



	}
}


一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值