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:

Initially, the collection contains n sets: {1}, {2}, {3}, . . . , {n}.

题意:

涉及到并查集的删除操作
1 合并这两个集合
2 将p移到q集合中去
3 统计元素个数和全部元素的和 相当于询问输出结果

5 7 ->5个集合,7种操作
1 1 2
2 3 4
1 3 5
3 4 ->3 12
2 4 1
3 4 ->3 7
3 3 ->2 8

#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll; const int N=1e5+;
int f[N*],ii[N*],num[N*];
ll sum[N*];
int n,m,cnt; void init()
{
for(int i=;i<=n;i++)
{
f[i]=i;
ii[i]=i;
sum[i]=i;
num[i]=;
}
cnt=n;
} int getf(int x)
{
if(f[x]==x)
return x;
return f[x]=getf(f[x]);
} void merge(int x,int y)
{
int t1=getf(ii[x]);
int t2=getf(ii[y]);
if(t1!=t2)
f[t2]=t1;
num[t1]+=num[t2];
sum[t1]+=sum[t2];
} void deletee(int x)
{
int p=ii[x];
sum[getf(p)]-=x;
num[getf(p)]--;
ii[x]=++cnt;
sum[ii[x]]=x;
num[ii[x]]=;
f[ii[x]]=ii[x];
} int main()
{
int op,p,q;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=;i<=m;i++)
{
scanf("%d",&op);
if(op==)//合并这两个集合
{
scanf("%d %d",&p,&q);
if(getf(ii[p])==getf(ii[q]))
continue;
else
merge(p,q);
}
else if(op==)//将p移到q集合中去
{
scanf("%d %d",&p,&q);
if(getf(ii[p])!=getf(ii[q]))//如果不在同一个集合中
{
deletee(p);
merge(p,q);
}
}
else if(op==)//统计元素个数和全部元素的和 相当于询问输出结果
{
scanf("%d",&p);
int k=getf(ii[p]);
printf("%d %lld\n",num[k],sum[k]);
}
}
}
return ;
}

UVA-11987-Almost Union-Find-并查集的基本操作合并、删除、移位的更多相关文章

  1. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  2. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...

  3. bzoj2733 / P3224 [HNOI2012]永无乡(并查集+线段树合并)

    [HNOI2012]永无乡 每个联通块的点集用动态开点线段树维护 并查集维护图 合并时把线段树也合并就好了. #include<iostream> #include<cstdio&g ...

  4. 【bzoj4668】冷战 并查集按秩合并+朴素LCA

    题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...

  5. BZOJ 3910 并查集+线段树合并

    思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...

  6. Dash Speed【好题,分治,并查集按秩合并】

    Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...

  7. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  8. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  9. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

随机推荐

  1. nodejs 在MYSQL 数据库中插入和查询数据

    插入前的数据库: 插入后的数据库: 输出结果: demo var mysql = require('mysql'); var connection = mysql.createConnection({ ...

  2. QueryList.class.php很方便的一个采集数据工具。

    QueryList.class.php是一个非常方面的网站数据采集工具.可以在这个上面采集别的网站数据. /*调用开始*/ require 'QueryList.class.php';header(' ...

  3. c++ exit() 函数

    函数用法 编辑 函数名: exit() 所在头文件:stdlib.h(如果是”VC6.0“的话头文件为:windows.h) 功 能: 关闭所有文件,终止正在执行的进程. exit(0)表示正常退出, ...

  4. [原创] delphi KeyUp、KeyPress、Keydown区别和用法,如何不按键盘调用事件

    KeyPress (Sender: TObject; var Key: Char);   当用户按下键盘上的字符键(字母,数字) 会触发该事件,功能键则不会(F1-F12,Ctrl,Alt,Shift ...

  5. NX二次开发-UFUN关闭本地文本文档uc4540

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_cfi.h> 5 #include <uf_ui.h> 6 7 us ...

  6. NXOpenC#_Training_intro(cn)【转载】

  7. C#利用栈实现字符串运算解析

    附上参考文章链接:https://blog.csdn.net/qq_34831781/article/details/80104219 本人整合修复一些bug后的代码 using System; us ...

  8. CodeForces-1221A-2048 Game-思维题

    You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every int ...

  9. Jsoup 学习笔记

    这里写自定义目录标题 Jsoup 学习笔记 解析 HTML 的字符串解析 URL 解析 本地文件解析 解析数据 DOM 解析 使用选择器解析 选择器概述 选择器组合用法 过滤用法 修改数据 HTML ...

  10. Hbase启动的时候出现:[RpcServer.handler=28,port=60000] ipc.RpcServer: RpcServer.handler=28,port=60000: exiting,master.HMasterCommandLine: Master exiting

    hadoop 版本:CDH5.02 Hbase 版本:hbase-0.96.1.1-cdh5.0.2 配置文件:hbase-site.xml <configuration> <pro ...