zoukankan      html  css  js  c++  java
  • Almost Union-Find 并查集(脱离原来的树)

    h: 0px; "> 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).
    Output
    For each type-3 command, output 2 integers: the number of elements and the sum of elements.
    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}
    Sample Input
    5 7
    1 1 2
    2 3 4
    1 3 5
    3 4
    2 4 1
    3 4
    3 3
    Sample Output
    3 12
    3 7
    2 8

     

    真是写残了,脑子都不转了,还好终于写过了,这道题是用到并查集,1,3比较好实现,就是2

    不太好实现,要把p加入q就得让p跟原来的树撇清关系,因为p可能是根节点,这样会让p整个集合归入q不合题意,所以需要一个数组这里用newid每次将p move掉然后给p一个新的id重新初始化一切有关的,

    还有就是fa数组保存的并不是父节点,要找父节点还是要用getf函数去找

     

     

    代码:

     

     

     

    #include <iostream>
    
    using namespace std;
    
    long long sum[200005];
    int count[200005],fa[200005],newid[200005];
    int n,m;
    void init()
    {
        for(int i=1;i<=n;i++)
            fa[i]=i,count[i]=1,sum[i]=(long long)i,newid[i]=i;
    }
    int getf(int x)
    {
        if(fa[x]!=x)fa[x]=getf(fa[x]);
        return fa[x];
    }
    void merge(int x,int y)
    {
        int xx=getf(newid[x]),yy=getf(newid[y]);
        fa[yy]=xx;
        sum[xx]+=sum[yy];
        count[xx]+=count[yy];
    }
    void move(int x)
    {
        int xx=getf(newid[x]);
        count[xx]--;
        sum[xx]-=(long long)x;
        newid[x]=++n;//
        fa[n]=n;//
        count[n]=1;///注意新指向的id父亲变成自己,成员和是x,这里就要求传入的参数就是输入的p本身,数量是1
        sum[n]=(long long)x;///此四行代表给x赋一个新的id以后他就通过这个id来完成各指令 这样原来的x就被切断了 不会保证他的儿子随着x合并到新的群体
    }
    int main()
    {
        int op,p,q;
        while(cin>>n>>m){
        init();
        while(m--)
        {
            cin>>op;
            if(op==3)
            {
                cin>>p;
                //cout<<newid[p]<<endl;
                cout<<count[getf(newid[p])]<<' '<<sum[getf(newid[p])]<<endl;///用getf
            }
            else
            {
                cin>>p>>q;
                if(getf(newid[p])==getf(newid[q]))continue;///这也是
                if(op==1)merge(p,q);
                else
                {
                    move(p);
                    merge(q,p);
                }
            }
        }
        }
    }
  • 相关阅读:
    HierarchicalDataTemplate
    Prism技巧
    常用Exception
    netcore URL重新路径
    Nintendo Switch相册整理
    交换变量-不借助任何变量
    QCombobox样式stylesheet总结
    Qt新旧Moc方式升级功能
    QSS使用方法总结
    【QRegExp】QLineEdit屏蔽空格
  • 原文地址:https://www.cnblogs.com/8023spz/p/7218741.html
Copyright © 2011-2022 走看看