并查集删除

题目描述了一种类似于并查集的数据结构,初始包含n个单独的集合。支持三种操作:1) 合并两个集合,2) 将元素移动到另一个集合,3) 查询集合的元素个数和总和。程序通过输入的n和m执行相应的操作,并对每个类型3的操作输出元素个数和总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
这道题的大意是给n个数和m个操作
1是合并,2是移动,3是输出
求所在分支的元素个数和元素之和。
用到了四个数组,sum用来记录当前集合里面元素的总和,cnt记录当前集合里面元素的个数,id用来记录当前点的编号。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=3000000;
int n,m,ans;
int f[N];
int cnt[N];
int sum[N];
int id[N];
int find(int x)
{
    if(f[x]==x)
        return x;
    return f[x]=find(f[x]);
}
void merge(int x,int y)
{
    int t1=find(x);
    int t2=find(y);
    if(t2!=t1)
    {
        f[t1]=t2;
        sum[t2]+=sum[t1];
        cnt[t2]+=cnt[t1];
    }
}
void move(int x)
{
    int fx=find(id[x]);
    cnt[fx]--;
    sum[fx]-=x;
    id[x]=++ans;
    f[ans]=ans;
    cnt[ans]=1;
    sum[ans]=x;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<=n*3;i++)
        {
            f[i]=i;
            sum[i]=i;
            cnt[i]=1;
            id[i]=i;
        }
        ans=n;
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d",&a);
            if(a==1)
            {
                scanf("%d%d",&b,&c);
                merge(id[b],id[c]);
            }
            if(a==2)
            {
                scanf("%d%d",&b,&c);
                if(find(id[b])!=find(id[c]))
                {
                    move(b);
                    merge(id[b],id[c]);
                }
            }
            if(a==3)
            {
                scanf("%d",&b);
                int fx=find(id[b]);
                printf("%d %d\n",cnt[fx],sum[fx]);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值