博客
关于我
程序设计基础18 并查集(一)
阅读量:378 次
发布时间:2019-03-05

本文共 2778 字,大约阅读时间需要 9 分钟。

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.

输入

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)

输出

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

样例输入

31 31 52 543 23 41 62 6

样例输出

45

一,思想方法:

本题目是一道典型的并查集问题。

1,注意的地方是在scanf()输入的第一轮Union后,想要对每个朋友圈的人数计数,不能采用

for (int i = 1; i <= max_id; i++) {    ++group_num[father(i)];}

而应当采用

for (int i = 1; i <= max_id; i++) {	++group_num[findFather(i)];}

原因是第一轮Union()后没有进行压缩彻底,例如1 3 和3 5,若是father[faA]=faB,数组会变成3 3 和3 5若是完全压缩应当是5 5 5 5

子函数如下:

int findFather(int x) {	int a = x;	while (x != father[x]) {		x = father[x];	}	while (a != father[a]) {		int z = a;		a = father[a];		father[z] = x;	}	return x;}void Union(int a, int b) {	int faA = findFather(a);	int faB = findFather(b);	if (faA != faB) {		father[faA] = faB;	}}

2,由于是多点测试,每次在输入之后都需要对father[]和group_num[]初始化,最好把初始化写在一个子函数里,看起来不冗余。

3,注意当输入的一个数据为0,即是0组朋友关系时,也要输出1,而不是0,就算没有一对朋友,也有一个人可以干活,因为题目规定了人数不是0.不要想当然。

二,我的代码

#include
#include
using namespace std;const int max_n = 10000010;int father[max_n] = { 0 };int group_num[max_n] = { 0 };int findFather(int x) { int a = x; while (x != father[x]) { x = father[x]; } while (a != father[a]) { int z = a; a = father[a]; father[z] = x; } return x;}void Union(int a, int b) { int faA = findFather(a); int faB = findFather(b); if (faA != faB) { father[faA] = faB; }}void init() { for (int i = 1; i <= max_n; i++) { father[i] = i; } for (int i = 1; i <= max_n; i++) { group_num[i] = 0; }}int main() { int N = 0; int x = 0, y = 0; while (scanf("%d", &N) != EOF) { int max_id = -1; if (N == 0) { printf("1\n"); continue; } init(); for (int i = 0; i < N; i++) { scanf("%d %d", &x, &y); if (x > max_id) { max_id = x; } if (y > max_id) { max_id = y; } Union(x, y); } for (int i = 1; i <= max_id; i++) { ++group_num[findFather(i)]; } int num = 0; for (int i = 1; i <= max_id; i++) { if (group_num[i] > num) { num = group_num[i]; } } printf("%d\n", num); } return 0;}

 

转载地址:http://uolwz.baihongyu.com/

你可能感兴趣的文章
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>