题目描述:

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. numpy 数组对象

    numpy 数组对象NumPy中的ndarray是一个多维数组对象,该对象由两部分组成:实际的数据,描述这些数据的元数据# eg_v1 import numpy as np a = np.arange ...

  2. oracel数据库主键自增

    -- Create sequence create sequence FILE_ID_SEQ   主键名(自增列) minvalue 1         起始 maxvalue 99999     最 ...

  3. John Deere Service Advisor EDL V2 Diagnostic Kit

    Support Languages: English, French, German, Italian, Portuguese, Russian, Spanish. John Deere Servic ...

  4. “Nested exception: 前言中不允许有内容"错误处理

    最近在做一个小项目,使用org.dom4j.DocumentHelper.parseText方法时一直报错”Nested exception: 前言中不允许有内容",这个parseText解 ...

  5. 2、Kafka架构

    Kafka架构图 1)Producer :消息生产者,就是向kafka broker发消息的客户端. 2)Consumer :消息消费者,向kafka broker取消息的客户端 3)Topic :可 ...

  6. Reading Lines from File in C++

    Reading Lines from File in C++ In C++, istringstream has been used to read lines from a file. code: ...

  7. MySQL 实战笔记

    01 | 基础架构:一条SQL查询语句是如何执行的? 大体可以分为: Server 层:包含了连接器.查询缓存.分析器.优化器.执行器,跨存储引擎的功能都在这一层实现的,比如存储过程.触发器.视图等. ...

  8. Windows Update Medic Service 拒绝访问

    修改注册表:HEKY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WaaSMedicSvc 中Start的值改为4.

  9. 20175312 2018-2019-2 《Java程序设计》第5周学习总结

    20175312 2018-2019-2 <Java程序设计>第5周学习总结 教材学习内容总结 已依照蓝墨云班课的要求完成了第六章的学习,主要的学习渠道是PPT,和书的课后习题. 总结如下 ...

  10. 决策树算法原理(ID3,C4.5)

    决策树算法原理(CART分类树) CART回归树 决策树的剪枝 决策树可以作为分类算法,也可以作为回归算法,同时特别适合集成学习比如随机森林. 1. 决策树ID3算法的信息论基础   1970年昆兰找 ...