• 图论基础之并查集的简单看法:HDU 1232 畅通工程&&More is better


                                                       HDU 1232 畅通工程http://acm.hdu.edu.cn/showproblem.php?pid=1232

    第一个并查集的题目:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int father[1000];
     6 int Find(int a)
     7 {
     8     if(a!=father[a])
     9         return Find(father[a]);
    10     return father[a];
    11 }
    12 void Union(int a,int b)
    13 {
    14     a=Find(a);
    15     b=Find(b);
    16     if(a!=b)
    17         father[a]=b;
    18 }
    19 int main()
    20 {
    21     int n,m,i,a,b,x;
    22     while(scanf("%d",&n)!=EOF)
    23     {
    24         if(n==0)
    25             break;
    26         scanf("%d",&m);
    27         for(i=1;i<=n;i++)
    28             father[i]=i;
    29         for(i=0;i<m;i++)
    30         {
    31             scanf("%d%d",&a,&b);
    32             Union(a,b);
    33         }
    34         x=0;
    35         for(i=1;i<=n;i++)
    36         {
    37             if(father[i]==i)
    38                 x++;
    39         }
    40         printf("%d
    ",x-1);
    41     }
    42     return 0;
    43 }

    反正就是一数组和俩函数(查找和连接)  具体看大牛的解析:http://hi.baidu.com/nicker2010/item/9b593c2019c720846f2cc3ee

                             More is betterhttp://acm.hdu.edu.cn/showproblem.php?pid=1856

     题意:一个老师想请学生帮忙 这些学生有只直接或者间接的朋友关系,老师只请他们中间的一群人帮忙 求最大的人数。

     1 #include<stdio.h>
     2 int pre[10000002];
     3 int count[10000002];//每个集合的人数
     4 int m;//集合中最大的人数
     5 int find(int x)
     6 {
     7     int r=x;
     8     while(pre[r]!=r)
     9         r=pre[r];
    10     return r;
    11 }
    12 int join(int x,int y)
    13 {
    14     if(count[x]>count[y])
    15     {
    16         pre[y]=x;
    17         count[x]+=count[y];
    18         if(count[x]>m)
    19             m=count[x];
    20     }
    21     else
    22     {
    23         pre[x]=y;
    24         count[y]+=count[x];
    25         if(count[y]>m)
    26             m=count[y];
    27     }
    28 return 0;
    29 }
    30 int main()
    31 {
    32     int n,i;
    33     int a,b;
    34     while(scanf("%d",&n)!=EOF)
    35     {
    36         m=1;
    37         for(i=0;i<10000002;i++)
    38         {
    39             pre[i]=i;
    40             count[i]=1;
    41         }
    42         
    43         for(i=0;i<n;i++)
    44         {
    45             scanf("%d %d",&a,&b);
    46             a=find(a);
    47             b=find(b);
    48             if(a!=b)
    49                 join(a,b);
    50         }
    51         printf("%d
    ",m);
    52     }
    53     return 0;
    54 }

     还有一份更牛逼的代码:

     1 #include<stdio.h>
     2 int m,rank[10000010],p[10000010],max;
     3 void Make_set()
     4 {
     5     int i;
     6     for(i=0; i<10000010; i++)
     7     {
     8        p[i]=i;
     9        rank[i]=1;
    10     }
    11 }
    12 int Find_set(int x)
    13 {
    14     if(p[x]!=x)
    15         p[x]=Find_set(p[x]);
    16     return p[x];
    17 }
    18 void Union(int x,int y)
    19 {
    20     p[x]=y;
    21     rank[y]+=rank[x];
    22     max=max>rank[y]?max:rank[y];
    23     rank[x]=0;
    24 }
    25 int main(){
    26     int x,y,i;
    27     while(scanf("%d",&m)!=EOF)
    28     {
    29         Make_set();
    30         max=0;
    31         for(i=0; i<m; i++)
    32         {
    33             scanf("%d%d",&x,&y);
    34             x=Find_set(x);
    35             y=Find_set(y);
    36             if(x!=y)
    37                 Union(x,y);
    38         }
    39         if(max)
    40             printf("%d
    ",max);
    41         else
    42             printf("1
    ");
    43     }
    44     }
  • 相关阅读:
    speex库音频降噪(含代码)
    语音增强(降噪)之一——谱减法
    CUDA与OpenGL互操作
    二维图像的三维旋转
    迷惑很久,仅以个人想法谈谈MVC架构,希望大家多给点意见
    Less使用心得
    js实现蛇形矩阵
    H5天气查询demo(二)
    HTML5 Shim
    html5发展的里程碑事件
  • 原文地址:https://www.cnblogs.com/tt123/p/3202960.html
Copyright © 2020-2023  润新知