zoukankan      html  css  js  c++  java
  • poj 2524 Ubinquitous Religions (并查集)

    依旧简单并查集,拿1611火速改了改火速提交,果断PE...把输出的最后一个空格放%d前面了,真活该不运行就交。

    但是这题有个疑问,为什么按秩合并没有比直接合并快??

    code: 

    #include<cstdio>
    int p[50005], r[50005] ;
    void make_set(int n){
        for(int i=0; i<n; i++){
            p[i] = i ;
            r[i] = 0 ;
        }
    }
    int find_set(int x){
        if(x!=p[x])
            p[x] = find_set(p[x]) ;
        return p[x] ;
    }
    void union_set(int x, int y){
        x = find_set(x) ;
        y = find_set(y) ;
        if(x!=y){
            if(r[x]>r[y])
                p[y] = x ;
            else{
                p[x] = y ;
                if(r[x]==r[y])
                    r[x] ++ ;
            }

        }
    }
    int main(){
        int n, m, i, x, y, sum, t=0 ;
        while(~scanf("%d%d", &n, &m)&&(n+m)){
            t ++ ;
            make_set(n) ;
            while(m--){
                scanf("%d", &x) ;
                scanf("%d", &y) ;
                union_set(x, y) ;
            }
            sum = 0 ;
            for(i=0; i<n; i++)
                if(i==find_set(i))
                    sum ++ ;
            printf("Case %d: %d\n", t, sum) ;
        }
        return 0 ;

    } 

  • 相关阅读:
    clickhouse-(04)-常用高阶函数
    clickhouse-(03)-库和表引擎
    clickhouse-(02)-适合的场景
    clickhouse-(01)-安装
    MySQL实战45讲-笔记
    Linux软连接和硬链接
    直接访问和间接访问
    指针和地址的区别
    配置Apache 运行CGI---------笔记
    配置Apache 运行CGI
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2332446.html
Copyright © 2011-2022 走看看