More is better
- 题目描述:
-
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:不能想着在找根的过程中数元素个数,因为不一定是叶子,数出来的不对,
随机推荐
- vue-cli 2.92版本 没有dev.server.js
在webpack.dev.conf.js 文件中 //首先 const express = require('express') const app = express() var appData = ...
- Java复制目录/子目录/文件
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- Ajax跨域请求COOKIE无法带上的解决办法
原生ajax请求方式: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xxxx.com/demo/b/ ...
- John Deere Service Advisor EDL V2 Diagnostic Kit
Support Languages: English, French, German, Italian, Portuguese, Russian, Spanish. John Deere Servic ...
- 解决 Bash On Windows 下载慢或无法下载的问题
解决 Bash On Windows "无法从 Windows 应用商店下载.请检查网络连接."的问题 Fiddler和Bash On Windows 源离线压缩包:http:// ...
- Filter中request对象强转问题
以下为过滤器doFIlter方法的源代码: public void doFilter(ServletRequest request, ServletResponse response, FilterC ...
- Flutter从零到∞学习笔记
有状态widget:StatefulWidget和无状态widget:StatelessWidget 前者不需要实现Widget build(BuildContext context). 具体的选择取 ...
- Python selectors实现socket并发
selectors模块 此模块允许基于选择模块原语构建高级别和高效的I / O多路复用. 鼓励用户使用此模块,除非他们想要精确控制使用的os级别的原语. 注:selectors也是包装了select高 ...
- Linux 系统开启最大线程数 调优
系统最大线程数说明 系统可开启的最大线程数,可根据系统本身负载配置进行调优. 查看系统最大线程数 1.查看系统开启的最大线程数. ulimit -u [root@izbp1brwu1w35r1dmj8 ...
- 剑指offer(48)不用加减乘除做加法
题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 题目分析 不用加减乘除做加法,我第一时间想到的就是用位运算,毕竟计算机是二进制的,所有的操作都是以位运算为基础 ...