Almost Union-Find 并查集(脱离原来的树)
h: 0px; "> 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,3比较好实现,就是2
不太好实现,要把p加入q就得让p跟原来的树撇清关系,因为p可能是根节点,这样会让p整个集合归入q不合题意,所以需要一个数组这里用newid每次将p move掉然后给p一个新的id重新初始化一切有关的,
还有就是fa数组保存的并不是父节点,要找父节点还是要用getf函数去找
代码:
#include <iostream> using namespace std; long long sum[];
int count[],fa[],newid[];
int n,m;
void init()
{
for(int i=;i<=n;i++)
fa[i]=i,count[i]=,sum[i]=(long long)i,newid[i]=i;
}
int getf(int x)
{
if(fa[x]!=x)fa[x]=getf(fa[x]);
return fa[x];
}
void merge(int x,int y)
{
int xx=getf(newid[x]),yy=getf(newid[y]);
fa[yy]=xx;
sum[xx]+=sum[yy];
count[xx]+=count[yy];
}
void move(int x)
{
int xx=getf(newid[x]);
count[xx]--;
sum[xx]-=(long long)x;
newid[x]=++n;//
fa[n]=n;//
count[n]=;///注意新指向的id父亲变成自己,成员和是x,这里就要求传入的参数就是输入的p本身,数量是1
sum[n]=(long long)x;///此四行代表给x赋一个新的id以后他就通过这个id来完成各指令 这样原来的x就被切断了 不会保证他的儿子随着x合并到新的群体
}
int main()
{
int op,p,q;
while(cin>>n>>m){
init();
while(m--)
{
cin>>op;
if(op==)
{
cin>>p;
//cout<<newid[p]<<endl;
cout<<count[getf(newid[p])]<<' '<<sum[getf(newid[p])]<<endl;///用getf
}
else
{
cin>>p>>q;
if(getf(newid[p])==getf(newid[q]))continue;///这也是
if(op==)merge(p,q);
else
{
move(p);
merge(q,p);
}
}
}
}
}
Almost Union-Find 并查集(脱离原来的树)的更多相关文章
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- poj2513--并查集+欧拉路+字典树
经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)
传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...
- 【并查集】BZOJ4551-[Tjoi2016&Heoi2016]树
NOIP太可怕了((( -口-) 题目链接 [题目大意] 给定一颗有根树(根为1),有以下两种操作: 1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个结点 ...
- E - Is It A Tree? 并查集判断是否为树
题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...
- BZOJ3673 & BZOJ3674 可持续化并查集 【可持续化线段树维护可持续化数组】
题目描述 n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0 输入格式 输出 ...
- BZOJ 3674 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Submit: 2515 Solved: 1107 [Submit][Sta ...
- bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】
bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...
- Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...
随机推荐
- angular5 ng-content使用方法
先自定义一个组件import { Component } from '@angular/core'; @Component({ selector: 'exe-greet', template: ` & ...
- 在linux环境下编译C++ 程序
单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: 单个源文件生成可执行程序 /* helloworld.cpp */ #include ...
- Filters: before, after, around, 常用到before_action,执行顺序是从外到内。
Filters是继承的,所以可以在ApplicationControlooer中设置filters. 如果有多个过滤,它们执行的顺序 先父类,然后往下推倒到子类 同一个类,按照代码上下顺序执行.
- 在 Confluence 6 中的 Jira 高级权限
启用嵌套用户组(Enable Nested Groups) 为嵌套组启用或禁用支持. 在启用嵌套用户组之前,你需要检查你在 JIRA 服务器中的嵌套用户组是否启用了.当嵌套用户组启用成功后,你可以将一 ...
- 使用opatch工具 打补丁Patch 21352635 -(Database Patch Set Update 11.2.0.4.8)
Patch 21352635 - Database Patch Set Update 11.2.0.4.8 一.OPatch工具检查及升级 OPatch工具包,在安装目录$ORACLE_HOME下,P ...
- JavaScript学习总结(十九)——使用js加载器动态加载外部Javascript文件
今天在网上找到了一个可以动态加载js文件的js加载器,具体代码如下: JsLoader.js 1 var MiniSite=new Object(); 2 /** 3 * 判断浏览器 4 */ 5 M ...
- Cattle学习笔记
Cattle学习笔记
- PHP:第三章——PHP中控制函数的函数
<pre> <?php header("Content-Type:text/html;charset=utf-8"); /******************** ...
- poj3020 二分图匹配 最大独立集
这是一道水题, 这里是最大流解法,之后再补 坑在又忘了反向建边了 题意:给你二维bool数组,让你求出能用多米诺骨牌覆盖所有 1 且骨牌最少的放法(因为多米诺骨牌1*2的结构方便描述,原题没有),原本 ...
- bzoj1215
题解: 暴力枚举每一种方案,然后hash判重 代码: #include<bits/stdc++.h> #define eps 1e-7 using namespace std; ],r[] ...