zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [zt]在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
    线程间操作无效: 从不是创建控件“...”的线程访问它。
    Unity IoC + WCF + wsHTTPBinding + Certificate Authorization
    [转]PowerDesigner使用教程 —— 概念数据模型
    C# Post数据和接收简单示例【摘】
    163相册验证码图片的识别手记之一 去除干扰
    实现WCF和Unity 的集成
    Html.ActionLink 几种重载方式说明及例子
    如何从程序员到项目经理【转帖51cto】
    OSG闪存
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3399922.html
Copyright © 2011-2022 走看看