并查集例题_并查集算法

并查集例题_并查集算法E – 带删除并查集 UVA – 11987 Almost Union-Find

大家好,又见面了,我是你们的朋友全栈君。

UVA – 11987 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). 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~n,n个数,初始每个数独自作为一个集合,然后进行m次操作。操作有三种:1 p q :把 p 所在的集合合并到 q 所在的集合

                                             2 p q :把 p 从 p 的集合中拿出,放到 q 的集合里

                                        3 p    :输出 p 所在的集合的元素个数和元素之和

思路:ma[x]=y 代表x在编号为y的集合里,fa[y]=z 代表编号为y的集合编号为z的集合同一连通分支(本来也是集合,但都说集合不太好分辨,并查集的部分就说连通分支吧)内(把集合当作个体来并查集),再用两个数组分别记录连通分支 i 内的数字的个数cou和数字的和sum

          这样的话对于1操作:fa[fx]=fy(fx是x所在的连通分支,fy是y所在的连通分支),//合并fx和fy

             cou[fy]+=cou[fx];  

            sum[fy]+=sum[fx]; 

             cou[fx]=0;  //清空fx

             sum[fx]=0; 

           2操作:cou[fx]–;

            cou[fy]++;
            sum[fy]+=x;
            sum[fx]-=x;
            ma[x]=ma[y];

        3操作:cou[fx] sum[fx]

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b) memset(a,b,sizeof(a))
#define fora(i,a,b) for(i=a;i<b;i++)
#define fors(i,a,b) for(i=a;i>b;i--)
#define fora2(i,a,b) for(i=a;i<=b;i++)
#define fors2(i,a,b) for(i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f

typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn= 100000+11;
map<int,int>ma;
int cou[maxn],sum[maxn];
int fa[maxn];
int n,m;
void init()
{
    ma.clear();
    int i;
    fora2(i,1,n)
    {
        fa[i]=i;
        cou[i]=1;
        sum[i]=i;
        ma[i]=i;
    }
}
int findx(int x)
{
    if(x==fa[x])return x;
    return fa[x]=findx(fa[x]);
}
int main()
{
    int kcase=0;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        while(m--)
        {
            int op;
            scanf("%d",&op);
            if(op==3)
            {
                int x;
                scanf("%d",&x);
                int fx=findx(ma[x]);
                printf("%d %d\n",cou[fx],sum[fx]);
                continue;
            }
            int x,y;
            scanf("%d%d",&x,&y);
            int fx=findx(ma[x]),fy=findx(ma[y]);
            if(fx==fy)continue;
            if(op==1)
            {
                //合并连通分支fx和fy
                fa[fx]=fy;
                cou[fy]+=cou[fx];
                sum[fy]+=sum[fx];
                //清空fx
                cou[fx]=0;
                sum[fx]=0;
                continue;
            }
            //把x从集合ma[x]拿出来
            sum[fx]-=x;
            cou[fx]--;
            //把x放到集合ma[y]
            ma[x]=ma[y];
            cou[fy]++;
            sum[fy]+=x;
            
            

        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/107acm/p/9430924.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107379.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月20日 下午12:20
下一篇 2022年4月20日 下午12:20


相关推荐

  • pageoffice集成

    pageoffice集成pageOffice 集成简述 下列简述是跨域请求 将文件服务器中的文件下载到本地 然后打开 保存之后再次上传到服务器 1 下载试用版本项目文件 Samples4 是 pageOffice 测试项目包 直接可在 TomcatWebApp 中运行 访问 localhost 8080 Samples4 index html 即可访问到 p

    2026年3月18日
    2
  • 使用instsrv和srvany注册windows系统服务

    使用instsrv和srvany注册windows系统服务1、下载配置instsrv和srvany下载地址:https://dl.pconline.com.cn/download/558946.html根据电脑属性复制文件:32位操作系统:将两个文件放入C:\Windows\System32文件夹下即可64位操作系统:除放入System32文件夹下,还需放入C:\Windows\SysWOW64文件夹下2、jar包和bat运行文件在同一目录下3、追加服务win+r打开运行窗口、输入cmd进入DOS窗口执行命令:instsrvr

    2022年6月14日
    48
  • matlab画图常用符号,matlab画图特殊符号[通俗易懂]

    matlab画图常用符号,matlab画图特殊符号[通俗易懂]在MATLAB中使用LaTex字符1.Tex字符表在text对象的函数中(函数title、xlabel、ylabel、zlabel或text),说明文字除使用标准的ASCII字符外,还可……matlab特殊字符_工学_高等教育_教育专区。本文说明了matlab中如何输入特殊字符,如希腊字母字符映射表C:\\WINDOWS\\system32\\charmap….

    2025年12月9日
    5
  • 用MDK生成bin文件的步骤及方法

    用MDK生成bin文件的步骤及方法1用MDK生成bin文件Embest徐良平在RVMDK中,默认情况下生成*.hex的可执行文件,但是当我们要生成*.bin的可执行文件时怎么办呢?答案是可以使用RVCT的fromelf.exe工具进行转换。也就是说首先将源文件编译链接成*.axf的文件,然后使用fromelf.exe工具将*.axf格式的文件转换成*.bin格式的文件。下面将具体

    2022年10月20日
    6
  • pycham2021版本激活码【在线破解激活】

    pycham2021版本激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    55
  • TerminateThread可能导致程序崩溃

    TerminateThread可能导致程序崩溃DWORD__stdcallmythread(void*){   while(true)   {      char*p=newchar[1024];      deletep;   }}int_tmain(intargc,_TCHAR*argv[]){   HANDLEh=CreateThread(NULL,0,mythread,N…

    2025年6月21日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号