南邮 OJ 1810 A. The more the better
2015-08-07 09:25
239 查看
A. The more the better
时间限制(普通/Java) : 5000 MS/ 15000 MS 运行内存限制 : 81920 KByte总提交 : 84 测试通过 : 21
比赛描述
Mr Chen wants some students to help him with a project. Because the project is rather complex, the more students come, the better it will be. Of course there are certain requirements.
Mr Chen selected a room big enough to hold the students. The student 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 Chen's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one student 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
提示
题目来源
ACM爱好者协会
/* head[i]:第i的人所在队伍的最小编号 num[i]:第i号人所在队伍的人数 maxNum:当前最大组的人数 */ #include<stdio.h> int head[10000001],num[10000001],maxNum,minNo,maxNo; int headNo(int i){ return head[i]==i?i:head[i]=headNo(head[i]); } void makeFriends(int i,int j){ if(i>j){ i^=j; j^=i; i^=j; } while(minNo>i){ minNo--; head[minNo]=minNo; num[minNo]=1; } while(maxNo<j){ maxNo++; head[maxNo]=maxNo; num[maxNo]=1; } int iHead=headNo(i),jHead=headNo(j); if(iHead==jHead){ return; } if(iHead>jHead){ head[iHead] = jHead; num[jHead] += num[iHead]; if(maxNum<num[jHead]){ maxNum = num[jHead]; } }else{ head[jHead] = iHead; num[iHead] += num[jHead]; if(maxNum<num[iHead]){ maxNum = num[iHead]; } } } int main(){ int i,j,n; while(scanf("%d",&n)==1){ minNo=maxNo=1; head[1]=1; num[1]=1; maxNum = 1; while(n--){ scanf("%d%d",&i,&j); makeFriends(i,j); } printf("%d\n",maxNum); } } /* 下面代码AC了不过时间较长 #include<stdio.h> int head[10000001],num[10000001],maxNum; int headNo(int i){ return head[i]==i?i:head[i]=headNo(head[i]); } void makeFriends(int i,int j){ int iHead=headNo(i),jHead=headNo(j); if(iHead==jHead){ return; } if(iHead>jHead){ head[iHead] = jHead; num[jHead] += num[iHead]; if(maxNum<num[jHead]){ maxNum = num[jHead]; } }else{ head[jHead] = iHead; num[iHead] += num[jHead]; if(maxNum<num[iHead]){ maxNum = num[iHead]; } } } int main(){ int i,j,n; while(scanf("%d",&n)==1){ for(i=1;i<=10000000;i++){ head[i] = i; num[i] = 1; } maxNum = 1; while(n--){ scanf("%d%d",&i,&j); makeFriends(i,j); } printf("%d\n",maxNum); } } */
相关文章推荐
- 关于singleInstance做的测试(亲测and接上篇文章继续测试)
- STL vector的简单用法
- Eclipse中问题大汇总及方案!
- J2EE--Servlet生命周期与原理
- 不一样的控制面板 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}
- PHP错误处理
- 如何在android style文件中使用自定义属性
- 创建mysql数据库
- ORACLE启动报错:ORA-16068:redo log file activation identifier mismatch
- 三十과 三十六
- mysql select语句
- JPA和Hibernate的区别
- Qt 4 迁移至 Qt 5
- 南邮 OJ 1799 比赛成绩查询问题
- 更复杂的滤镜
- java如何根本生成10位号(比如订单号)
- struts2工作机制详解
- mysql where语句
- mysql 排序修改删除
- 关于http的gzip解压