求大神指点杭电1586more is better,答案对,就是超时(⊙o⊙)求指点

More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 19936    Accepted Submission(s): 7313

Problem 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)
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

Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect).In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.

#include <iostream> using namespace std; int arr[10000001] ; int num[10000001] ; int Max; inline int find(int r) {while(r!=arr[r])r=arr[r];return r; } inline void judge(int a,int b) {int x,y;x=find(a);y=find(b);if(x!=y){arr[y]=x;num[x]=num[y]+num[x];num[y]=num[x];if(num[y]>Max)Max=num[y];}     } inline void Init() {for(int i=1;i<= 10000001 ;i++){arr[i]=i;num[i]=1;} } int main() {int N,a,b;while (cin>>N){Init();Max=num[1];while(N--){cin>>a>>b;judge(a, b);}cout<<Max<<endl;}return 0; }

杭电1856More is better相关推荐

  1. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  2. 【ACM】杭电OJ 1106 函数atoi

    函数atoi是把字符串转化成整数的函数,头文件为 #include "stdlib.h" e.g. 运行环境:Dev-C++ 5.11 杭电1106 调用了sort函数,运行的时间 ...

  3. 【ACM】杭电OJ 2037

    题目链接:杭电OJ 2037 先把b[i]进行排序,然后,b[i]与a[i+1]进行比较. #include <iostream> #include <cstdio> #inc ...

  4. 【ACM】杭电OJ 2020(排序)

    题目链接:杭电OJ 2020 排序可以有冒泡排序,选择排序,或者直接调用函数. 下面是选择排序: #include <stdio.h> #include <math.h> in ...

  5. 【ACM】杭电OJ 2018

    题目链接:杭电OJ 2018 从n>4开始,每一年的牛的数量=前一年的牛的数量+三年前的牛的数量 问:为什么是三年前? 答:假设三年前有一头小牛出生,出生的那一年即为第一年,到了第四年,即三年后 ...

  6. 【ACM】杭电OJ 1005

     题目链接:杭电OJ 1005 超时代码如下(而且开辟的数组空间大小不够): #include <stdio.h> int m[100000]; int f(int n,int a,int ...

  7. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  8. 【ACM】杭电OJ 2012。

    题目链接:杭电OJ 2012 思路很简单,但是有一种高效算法显示编译错误,不知道为什么 运行环境:VS2017 AC代码: #include <stdio.h> #include < ...

  9. 【ACM】杭电OJ 1003。

    运行环境VS2017  题目链接:杭电OJ 1003 主要思想是: 用d[i]来存放前i项中最大的和,得到end,然后再倒推,得起始的位置begin 然而在程序42行的疑问,大家可以讨论一下吗???? ...

最新文章

  1. Python OS模块使用汇总
  2. python数据展示平台_Python获取各大地图平台经纬度数据,哪家的数据最准确?
  3. JavaScript 仿LightBox内容显示效果
  4. 英文文章如何进行略读
  5. UEFI下面安装win7+Xubuntu18.10双系统
  6. 剑指offer之矩阵中的最短路径
  7. 【剑指Offer学习】【面试题22:栈的压入、弹出序列】
  8. Android自定义View【实战教程】3⃣️----Paint类、Path类以及PathEffect类详解
  9. 镜像文件下载及VM部署win xp 保姆级教程
  10. 吃鸡显示连接服务器超时,吃鸡 怎么显示连接超时 | 手游网游页游攻略大全
  11. pdf转jpg在线转换的方法分享
  12. PS去除图片中文字的方法详细图文教程
  13. 用银行卡号查相应的归属银行,卡种类
  14. js判断浏览器是pc端还是手机端
  15. python金融分析小知识(6)——偏度与峰度的计算
  16. gem install factory_girl
  17. 深信服应用交付(AD)学习笔记
  18. 去掉word页眉上横线的技巧
  19. Shell编程基本指令
  20. 如何实现windows自带输入法字体任意更改

热门文章

  1. 「神策 2020 数据驱动用户大会」10 月 13 日即将开幕,5 大亮点提前解锁!
  2. 2017 数据驱动大会赠票赠书名单出啦!
  3. 神策沙龙回顾:大数据技术和金融、房产、理财的深度结合
  4. 他们是最懂数据的商家!智能品牌时代到来
  5. Transition 实现轮播图
  6. JPA Hibernate应用实例
  7. 计算 1!+2!+3!+……+10!
  8. X86/ARM 模拟器
  9. if you miss it, you can hope for future
  10. apple watch自身不会让你更健康,而这些会