并查集例题_并查集算法

并查集例题_并查集算法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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 实验七 香农编码_香农编码效率可以大于1吗

    实验七 香农编码_香农编码效率可以大于1吗一、实验目的编程,对某一离散无记忆信源实现香农编码,输出消息符号及其对应的码字。设离散无记忆信源,。二进制香农编码过程如下:1、将信源发出的N个消息符号按其概率的递减次序依次排列。2、按下式计算第i个消息的二进制代码组的码长,并取整。3、为了编成唯一可译码,首先计算第i个消息的累加概率4、将累加概率Pi(为小数)变成二进制数5、除去小数点,并根据码长li,取小数点后li位数作为第i个消息的码字。二、实验环境Dev三、实验过程:#include<stdio.h>

    2025年10月18日
    4
  • ie浏览器无法连接到代理服务器_网页无法连接到代理服务器

    ie浏览器无法连接到代理服务器_网页无法连接到代理服务器由于工作上的需要,相信很多用户会使用IE代理服务器,但是在设置之后遇到IE代理服务器没有响应错误提示(如图所示),并且浏览器无法打开网页的问题,但使用其他浏览器是可以正常上网,出现这种情况很有可能是注册表设置问题,我们可以按照以下方式来解决。     1、打开运行窗口(系统键Win+R),输入”regedit.exe”.如下图所示,根据下边的地址逐步打开:HKE

    2025年8月11日
    3
  • python猪脸识别_一种猪脸的识别方法与流程

    python猪脸识别_一种猪脸的识别方法与流程本发明涉及人工智能技术领域,特别涉及到一种用于猪脸的自动识别方法。背景技术:当前养猪场进行批量养猪的过程中,养殖者需要掌握每头猪只的饮食情况、健康状态、生长状况以及情绪等信息,因此识别每头猪只的身份信息为养殖者掌握养殖场基本状况提供便利,目前大型养猪场对于猪只的身份管理没有一个准确有效的识别方法,使得在管理猪只的过程中出现混乱和错误的情况,因此,猪脸识别技术的缺乏不利于规模化的精准养猪的推广。技术…

    2022年6月21日
    31
  • 将一个接口响应时间从2s优化到 200ms以内的一个案例

    将一个接口响应时间从2s优化到 200ms以内的一个案例一 背景在开发联调阶段发现一个接口的响应时间特别长 经常超时 囧 本文讲讲是如何定位到性能瓶颈以及修改的思路 将该接口从 2s 左右优化到 200ms 以内 二 步骤 2 1 定位定位性能瓶颈有两个思路 一个是通过工具去监控 一个是通过经验去猜想 2 1 1 工具监控就工具而言 推荐使用 arthas 用到的是 trace 命令具体安装步骤很简单 大家自行研究 我的使用步骤是

    2025年7月7日
    4
  • 38款 流媒体服务器开源软件

    38款 流媒体服务器开源软件Flash流媒体服务器Red5Red5是一个采用Java开发开源的Flash流媒体服务器。它支持:把音频(MP3)和视频(FLV)转换成播放流;录制客户端播放流(只支持FLV);共享对象;现场直播流发布;远程调用。Red5使用RSTP作为流媒体传输协议,在其自带的一些示例中演示了在线录制,flash…更多Red5信息最近更新:Red51.0.1

    2022年5月2日
    43
  • 配置JAVA_HOME「建议收藏」

    配置JAVA_HOME「建议收藏」配置JAVA_HOME1、新建系统环境变量JAVA_HOME变量值为C:\ProgramFiles\Java\jdk-12.0.12、编辑Path添加%JAVA_HOME%\bin3、新建系统环境变量CLASSPATH,变量值为.;%Java_Home%\bin;%Java_Home%\lib\dt.jar;%Java_Home%\lib\tools.jar1、新建系统环境变量JAVA_HOM…

    2022年6月9日
    29

发表回复

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

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