并查集例题_并查集算法

并查集例题_并查集算法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


相关推荐

  • potplayer软件安装与常用配置

    potplayer软件安装与常用配置potplayer软件安装与常用配置1安装potplayer是一款十分好用的视频播放器,可以播放很多格式的视频,自定义皮肤,任意倍速,截取视频片段,还可以播放直播源(比如中国的CCTV以及各大卫视)…potplayer网址下载安装即可,最后如果显示要安装插件的话,去掉勾选即可2使用设置无边框:首先打开设置界面(快捷键F5),勾选如下两项设置打开视频时,默认居中显示,非全屏:首先打开设置界面(快捷键F5),进行如下两项设置进度条缩略图:首先打开设置界面(快捷键F5

    2022年5月11日
    98
  • 一款不错的Linux命令行下的FTP客户端软件

    一款不错的Linux命令行下的FTP客户端软件文章作者 张宴本文版本 v1 0 最后修改 2008 12 12 转载请注明原文链接 http blog s135 com post 387 一款 Linux 命令行下的 FTP 客户端软件 ncftp 用来作整个目录批量上传 ncftpput 是它的一个附带程序 在 shell 脚本中调用 ncftpput 上传文件到 FTP 服务器 非常方便 详细内容见其官方网站 http

    2026年3月26日
    2
  • Vim配置文件vimrc入门介绍

    Vim配置文件vimrc入门介绍本文转载自:vim教程网Vim入门级基础配置-Vim入门教程(1)介绍Vim配置文件.vimrc,配置Vim显示行号、支持utf8中文不乱码、突出显示Vim当前行,设置高亮显示括号匹配和tab缩进,解决Vim粘贴时多出缩进和空格问题。一、Vim配置文件.vimrcVim编辑器相关的所有功能开关都可以通过.vimrc文件进行设置。.vimrc配置文件分系统配置和用户配置两种。系…

    2022年4月30日
    111
  • 腾讯收购冰川网络_冰河是谁

    腾讯收购冰川网络_冰河是谁应朋友的邀约,不久前去腾讯交流学习了。这次的收获还是蛮大的,今天,跟小伙伴们分享下这次去腾讯交流和学习的体会。

    2022年8月22日
    12
  • 盘点当下大热的 7 大 Github 机器学习『创新』项目

    盘点当下大热的 7 大 Github 机器学习『创新』项目本文将会分享近期发布的七大GitHub机器学习项目。这些项目广泛覆盖了机器学习的各个领域,包括自然语言处理(NLP)、计算机视觉、大数据等。最顶尖的Github机器学习项…

    2022年6月7日
    37
  • 计算机网络原理实验(三)——小型校园网络模拟搭建

    计算机网络原理实验(三)——小型校园网络模拟搭建计算机网络原理实验 三 小型校园网络模拟搭建前言基础知识拓扑图网络地址划分终端设备配置三层交换机配置路由器 A 配置路由器 B 二层交换机配置服务器网页内容运行截图使用技巧心得体会前言思科路由模拟器 CiscoPacketT 模拟出 pc 若干 路由器两台 两层及三层交换机各一台 网线若干 网络信息 某学校有 3 个学院 学院 A 有网络用户 50 人 学院 B 有网络用户 230 人 学院 C 有 5

    2026年3月20日
    2

发表回复

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

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