您的位置:首页 > 其它

南邮 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);
}
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: