题目描述:

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.

样例输入:
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
样例输出:
4
2
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define N 10000001
//将一个数定义在预处理部分
using namespace std;
int path[N];
int sum[N];
int findroot(int a){
int temp=a;
while (path[a] != -){
a=path[a];
}
int temp2;
//改进,使树的高度变矮,宽度增加,方便找根
while (path[temp]!= -){
temp2=path[temp];
path[temp]=a;
temp=temp2;
}
return a;
} int main (){
int n;
while (cin>>n){
for (int i=;i<=N;i++){
path[i]=-; sum[i]=;
}
int ans=;
int a,b;
while (n--){
cin >>a>>b;
a=findroot(a);
b=findroot(b);
if (a!=b){
path[a]=b;
sum[b]+=sum[a];
sum[a]=;
}
}
for (int i=;i<=N;i++){
if (sum[i] > ans)
ans =sum[i];
}
cout<<ans<<endl;
}
return ;
}

跟上一道畅通工程几乎一样

加了一个在合并集合时同时合并(加)元素个数的功能

ps:不能想着在找根的过程中数元素个数,因为不一定是叶子,数出来的不对,

随机推荐

  1. Python数据分析Numpy库方法简介(二)

    数据分析图片保存:vg 1.保存图片:plt.savefig(path) 2.图片格式:jpg,png,svg(建议使用,不失真) 3.数据存储格式: excle,csv csv介绍 csv就是用逗号 ...

  2. 2.0JAVA基础复习——JAVA语言的基础组成关键字和标识符

    JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...

  3. php 禁止屏蔽类

    1.禁止右键 <body oncontextmenu=return(false)> 2.禁止选择 <body onselectstart="return false&quo ...

  4. Linux 系统TCP连接内存大小限制 调优

    系统TCP连接内存大小限制 TCP的每一个连接请求,读写都需要占用系统内存资源,可根据系统配置,对TCP连接数,内存大小,限制调优. 查看系统内存资源 记录内存 详情:cat /proc/meminf ...

  5. docker启动容器报错: could not synchronise with container process: not a directory

    错误现象 在运行容器时,出现以下错误 [root@localhost test]# docker run -it -d -v $PWD/test.txt:/mydir mytest fd44cdc55 ...

  6. Injection的简单辨析

    依赖注入(injection)是一种对任何编程语言都有效的概念.依赖注入背后的一般概念称为控制反转.根据这个概念,类不应该静态配置其依赖项,而应该从外部配置. 如果Java类使用此类的实例,则Java ...

  7. Win32汇编学习(9):窗口控件

    这次我们将探讨控件,这些控件是我们程序主要的输入输出设备. 理论: WINDOWS 提供了几个预定义的窗口类以方便我们的使用.大多数时间内,我们把它们用在对话框中,所以我们一般就它们叫做子窗口控件.子 ...

  8. 论文笔记:Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships

    Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships ...

  9. LeetCode in action

    (1) Linked List: 2-add-two-numbers,2.cpp 19-remove-nth-node-from-end-of-list,TBD 21-merge-two-sorted ...

  10. FastAdmin笔记~

    在phpstud命令行输入以下命令! 1.在命令行一键生成test表格的crud php think crud -t  test 2.在数据库创建表必须加上表注释 3.生成test控制器的权限菜单 p ...