zoukankan      html  css  js  c++  java
  • poj 1988 多校联赛 带权并查集

      这个题的意思是初始给你N个砖块, 维护两个操作, M a, b表示将含有a的堆放到b堆得上面(a和b在同一堆得话那么就忽略此操作, 有种并查集的感觉), C a查询a下面有几个砖块, 其实我们可以使用带权的并查集来维护这个题, 增加两个变量under[i]表示i下面有几个装快, cnt[i]表示以i为底的堆有几个砖块, 然后我们就可以在并查集Find函数回溯的时候就可以更新under数组。。 代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 30000 + 100;
    int par[maxn];
    int cnt[maxn], under[maxn];
    
    void init(int n)
    {
        for(int i=1; i<=n; i++)
        {
            par[i] = i;
            cnt[i] = 1;
            under[i] = 0;
        }
    }
    
    int Find(int x)
    {
        if(x == par[x]) return x;
        else
        {
            int t = Find(par[x]);
            under[x] += under[par[x]];    //加过之后 路径压缩
            return par[x]=t;
        }
    }
    
    void Union(int x, int y)
    {
        x = Find(x); y = Find(y);
        if(x != y)    //x摞在y上
        {
            under[x] = cnt[y];
            cnt[y] += cnt[x];
            par[x] = y;
        }
    }
    
    char s[10];
    
    int main()
    {
        int N;
        while(scanf("%d", &N) != EOF)
        {
            init(30000);
            for(int i=0; i<N; i++)
            {
                scanf("%s", s);
                if(s[0] == 'M')
                {
                    int a, b;
                    scanf("%d%d", &a, &b);
                    Union(a, b);
                }
                else
                {
                    int a;
                    scanf("%d", &a);
                    Find(a);
                    printf("%d
    ", under[a]);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    ●单例模式
    ●扩展方法
    ●存储过程比sql语句慢
    ●rownum() over()
    ●日期格式化
    ●sql优化
    VS建立Web网站 20141201
    ORM操作(一) 20141128
    流的操作20141104
    控件:菜单、工具栏、状态栏及TreeView的操作 20141103
  • 原文地址:https://www.cnblogs.com/xingxing1024/p/5267993.html
Copyright © 2011-2022 走看看