zoukankan      html  css  js  c++  java
  • UVA

    Almost Union-Find

    I hope you know the beautiful Union-Find structure. In this problem, you're to implement something similar, but not identical.

    The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:

    1 p q

    Union the sets containing p and q. If p and q are already in the same set, ignore this command.

    2 p q

    Move p to the set containing q. If p and q are already in the same set, ignore this command

    3 p

    Return the number of elements and the sum of elements in the set containing p.

    Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.

    Input

    There are several test cases. Each test case begins with a line containing two integers n and m (1<=n,m<=100,000), the number of integers, and the number of commands. Each of the next m lines contains a command. For every operation, 1<=p,q<=n. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

    Output

    For each type-3 command, output 2 integers: the number of elements and the sum of elements.

    Sample Input

    5 7
    1 1 2
    2 3 4
    1 3 5
    3 4
    2 4 1
    3 4
    3 3
    

    Output for the Sample Input

    3 12
    3 7
    2 8
    

    Explanation

    Initially: {1}, {2}, {3}, {4}, {5}

    Collection after operation 1 1 2: {1,2}, {3}, {4}, {5}

    Collection after operation 2 3 4: {1,2}, {3,4}, {5} (we omit the empty set that is produced when taking out 3 from {3})

    Collection after operation 1 3 5: {1,2}, {3,4,5}

    Collection after operation 2 4 1: {1,2,4}, {3,5}


    Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
    Special Thanks: Yiming Li
    Note: Please make sure to test your program with the gift I/O files before submitting!

    ac代码:

    #include<stdio.h>
    int f[200005],id[200005],c[200005],sum[200005];
    int dex;
    int find(int x)
    {
        return f[x]==x?x:f[x]=find(f[x]);
    }
    void join(int x,int y)
    {
        int fx=find(x),fy=find(y);
        if(fx!=fy){
            f[fy]=fx;
            c[fx]+=c[fy];
            sum[fx]+=sum[fy];
        }
    }
    void del(int x)
    {
        int fx=find(id[x]);
        c[fx]--;
        sum[fx]-=x;
        id[x]=++dex;
        f[dex]=dex;
        c[dex]=1;
        sum[dex]=x;    //并查集删除操作
    }
    int main()
    {
        int n,q,x,y,z,i;
        while(~scanf("%d%d",&n,&q)){
            dex=n;
            for(i=1;i<=n;i++){
                f[i]=i;
                id[i]=i;
                c[i]=1;
                sum[i]=i;
            }
            for(i=1;i<=q;i++){
                scanf("%d",&x);
                if(x==1){
                    scanf("%d%d",&y,&z);
                    join(id[y],id[z]);
                }
                else if(x==2){
                    scanf("%d%d",&y,&z);
                    int fy=find(id[y]);
                    int fz=find(id[z]);
                    if(fy!=fz){
                        del(y);
                        join(id[y],id[z]);
                    }
                }
                else{
                    scanf("%d",&y);
                    int fy=find(id[y]);
                    printf("%d %d
    ",c[fy],sum[fy]);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    [考试]20151017数据结构
    [考试]20151016动态规划
    [考试]20151015分治
    [BZOJ1501/NOI2005]智慧珠游戏
    [BZOJ3139/HNOI2013]比赛
    [考试]20151013搜索
    BZOJ3082: Graph2
    BZOJ4690: Never Wait for Weights
    BZOJ4668: 冷战
    BZOJ3262: 陌上花开
  • 原文地址:https://www.cnblogs.com/yzm10/p/7224496.html
Copyright © 2011-2022 走看看