UVA - 11987 Almost Union-Find (并查集)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3138点击打开链接

I hope you know the beautiful Union-Find structure. In this problem, you’re to implement somethingsimilar, 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 containingp.Initially, the collection contains n sets: {1}, {2}, {3}, . . . , {n}.InputThere 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 linescontains a command. For every operation, 1 ≤ p, q ≤ n. The input is terminated by end-of-file (EOF).OutputFor each type-3 command, output 2 integers: the number of elements and the sum of elements.ExplanationInitially: {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 whentaking 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 Input5 71 1 22 3 41 3 53 42 4 13 43 3Sample Output3 123 72 8




题目真jb丑。。懒得弄了 看链接吧

这道题除了询问有两个操作  一个是将个体合并 一个是集合合并 网络上大部分是删除操作 

我的想法是将数组扩大一倍 也就是将每个数映射到最大边界 然后形成一个两层的结构 具体画个图吧




从左到右对应1234吧

然后现在将23单独合并 (注意这里不是集合合并)

于是变成了



之后 我们将2所在的集合(3也是一样 因为他们延伸上去寻找的地方都是同为2的上一层)和1进行集合合并


这样就保证了 在合并的过程中 也将集合里的其他元素合并进去


这里编号3的上层应该与上上层有一个对应关系这里把他删了。。 没有实际意义 


而这些映射可以通过数组来实现 这里麻烦的开了100, 000+ i 


可以用其他数组储存对应关系

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
long long int pre[222222];
long long int sum[222222];
long long int num[222222];
int findx(int x)
{
    int r=x;
    while(pre[r]!=r)
    {
        r=pre[r];
    }
    int i=x;int j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}
void join (int x,int y)
{
    int p1=findx(x);
    int p2=findx(y);
    if(p1!=p2)
    {

        pre[p2]=p1;
        sum[p1]+=sum[p2];
        sum[p2]-=sum[p2];
        num[p1]+=num[p2];
        num[p2]-=num[p2];
    }
}
void getnum(int x,int y)
{
    int p1=findx(x);
    int p2=findx(y);
    if(p1!=p2)
    {

        pre[y]=p1;
        sum[p1]+=y;
        sum[p2]-=y;
        num[p1]+=1;
        num[p2]-=1;
    }
}
int main()
{
   int n=0;int m=0;
   while(~scanf("%d%d",&n,&m))
   {
       memset(pre,0,sizeof(pre));
       memset(sum,0,sizeof(sum));
       memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
            {
                pre[i]=100000+i;
                pre[i+100000]=100000+i;
            }
        for(int i=100001;i<=100000+n;i++)
        {
            pre[i]=i;
            sum[i]=i-100000;
            num[i]=1;
        }
        for(int i=0;i<m;i++)
        {
            int mid=0;
            scanf("%d",&mid);
            if(mid==1)
            {
                int mid1;int mid2;
                scanf("%d%d",&mid1,&mid2);
                join(mid1,mid2);
            }
            else if(mid==2)
            {
                int mid1;int mid2;
                scanf("%d%d",&mid1,&mid2);
                getnum(mid2,mid1);
            }
            else
            {
                int mid1;
                scanf("%d",&mid1);
                printf("%lld %lld\n",num[findx(mid1)],sum[findx(mid1)]);
            }
        }

   }
}


2017.8.30更新:最近接触到种类并查集(带权并查集) 其中的一种解决带权并查集的方法就是类似于上面的思想 将数组扩大常数倍 然后分层进行并查集

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值