Hud5.1.4 1856 More is better

来源:互联网 发布:汽车维修模拟软件 编辑:程序博客网 时间:2024/05/16 11:02

Hud5.1.4    1856     More is better

Time Limit: 5000/1000 MS (Java/Others)Memory Limit: 327680/102400 K (Java/Others)

Total Submission(s): 190 AcceptedSubmission(s): 78

 

Problem Description

Mr Wang wants some boys to help him with aproject. Because the project is rather complex, the more boys come, the betterit will be. Of course there are certain requirements.

 

Mr Wang selected a room big enough to holdthe 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 verybeginning. After Mr Wang's selection any two of them who are still in this roomshould be friends (direct or indirect), or there is only one boy left. Givenall the direct friend-pairs, you should decide the best way.

 

 

Input

The first line of the input contains aninteger n (0 ≤ n ≤ 100 000) - the number of directfriend-pairs. The following n lines each contains a pair of numbers A and B separatedby 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 oneinteger 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), Band C are friends(direct or indirect),

then A and C are also friends(indirect).

 

 Inthe 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.

 

 

题解:

这道题是交朋友,有n对朋友,求最大的朋友圈中有多少个人。

这道题是用并查集,因为其中有n对朋友,因为这道题中人物的序号较大有10000000,开怎么大的数组明显不划算,所以需要在进行并查集之前进行简化。化简之后最多有200000个人只需要开辟一个200000的数组,浪费的空间少点。先将所有的朋友输入,然后统计有多少不同的人m,按二分查找就可以找到输入的朋友的位置,该位置代表这个人,不同的人有不同的位置,不会出现重复。然后就是进行初始化,和关系的合并。这道题由于输入的数据量较大,所以要用scanf()。一开始用cin,超时,用scanf只用了300+ms。

 

源代码:

#include <iostream>

#include <string>

#include <cstring>

#include <algorithm>

using namespace std;

#define MAXINT 100500

 

struct node

{

       intparent,rank;

} boy[200050];

 

struct nd

{

       inta,b;

}temp[100050];

 

int st[200050];

 

int reset(int x,int m)

{

       intl = 0,r = m - 1,mid = l + r;

       while(l<= r)

       {

              if(x< st[mid])

              {

                     r= mid - 1;

                     mid= (r+l)/2;

              }

              elseif(x > st[mid])

              {

                     l= mid + 1;

                     mid= (r+l)/2;

              }

              else

                     returnmid;

       }

}

 

void init(int m,int n)

{

       for(inti = 0;i < m;i++)

       {

              boy[i].parent= i;

              boy[i].rank= 1;

       }

}

 

int find_parent(int x)

{

       if(boy[x].parent== x)

              returnx;

      

       returnboy[x].parent = find_parent(boy[x].parent);

}

 

void Union(inta,int b)

{

       a= find_parent(a);

       b= find_parent(b);

 

       if(a== b)

              return;

 

       if(boy[a].rank> boy[b].rank)

       {

              boy[b].parent= a;

              boy[a].rank+= boy[b].rank;

       }

       else

       {

              boy[a].parent= b;

              boy[b].rank+= boy[a].rank;

       }

}

 

int main()

{

       intn;

       while(cin>> n)

       {

              intj = 0;

              for(inti = 0;i < n;i++)

              {

                     scanf("%d%d",&temp[i].a,&temp[i].b);

                     st[j++]= temp[i].a;

                     st[j++]= temp[i].b;

              }

 

              sort(st,st+j);

 

              intm = 1;

              for(inti = 1;i < j;i++)

              {

                     if(st[i]!= st[i-1])

                            st[m++]= st[i];

              }

 

              init(m,n);

       int c,d;

              for(inti = 0;i < n;i++)

              {

                  c = reset(temp[i].a,m);

                      d = reset(temp[i].b,m);

                     Union(c,d);

              }

 

              intMAX = 0;

              for(inti = 0;i < m;i++)

              {

                     if(boy[i].parent== i)

                            MAX= max(MAX,boy[i].rank);

              }

 

              cout<< MAX << endl;

       }

}