• More is better 并查集最原始 最有效的形式,用rank数组,(结构体)


    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
    ***************************************************************************************************************************
    并查集求最大的连通块
    ***************************************************************************************************************************
     1 /*
     2 并查集最原始最有效的形式
     3 有rank数组的
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstring>
     8 #include<cmath>
     9 #include<cstdio>
    10 #define maxn 100005
    11 using namespace std;
    12 struct node
    13 {
    14     int fa;
    15     int rank;
    16 }no[maxn+1];
    17 int max1;
    18 void init()
    19 {
    20     for(int it=0;it<=maxn;it++)
    21     {
    22         no[it].fa=it;
    23         no[it].rank=1;
    24     }
    25 }
    26 int find(int x)
    27 {
    28     int temp=x,root;
    29     while(no[x].fa!=x)
    30      x=no[x].fa;
    31     root=x;
    32     x=temp;
    33     while(no[x].fa!=x)
    34     {
    35         temp=no[x].fa;
    36         no[x].fa=root;
    37         x=temp;
    38     }
    39     return root;
    40 }
    41 int Unon(int x,int y)
    42 {
    43     int a,b;
    44     a=find(x);
    45     b=find(y);
    46     if(no[a].rank>=no[b].rank)
    47     {
    48         no[b].fa=no[a].fa;
    49         no[a].rank+=no[b].rank;
    50         if(max1<no[a].rank)
    51           max1=no[a].rank;
    52     }
    53     else
    54     {
    55         no[a].fa=no[b].fa;
    56         no[b].rank+=no[a].rank;
    57         if(max1<no[b].rank)
    58          max1=no[b].rank;
    59     }
    60 }
    61 int main()
    62 {
    63     int n,a,b,x,y;
    64     while(scanf("%d",&n)!=EOF)
    65     {
    66         init();
    67         max1=-1;
    68         while(n--)
    69         {
    70             scanf("%d %d",&a,&b);
    71             x=find(a);
    72             y=find(b);
    73             if(x!=y)
    74               Unon(a,b);
    75         }
    76         if(max1!=-1)
    77           printf("%d
    ",max1);
    78         else
    79           printf("1
    ");
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    导入动态页面的两种方法
    JSTL之c:set
    CentOS更新源
    MVC轻量web应用
    Linux设备驱动开发流程(转)
    g++: internal compiler error: Killed (program cc1plus)Please submit a full bug report,内存不足问题解决
    .PHONY的作用
    CMake(转)
    关于a+++++b含义的问题
    返回值为函数指针的函数(转)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3399922.html
Copyright © 2020-2023  润新知