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所在集 ...
随机推荐
- Codeforces 260B - Ancient Prophesy
260B - Ancient Prophesy 思路:字符串处理,把符合条件的答案放进map里,用string类中的substr()函数会简单一些,map中的值可以边加边记录答案,可以省略迭代器访问部 ...
- C#通过Oracle.ManagedDataAccess无法访问Oralce
问题描述:通过C#引用Oracle.ManagedDataAccess.dll访问Oracle,写了如下一段代码,在本机能正常访问,但是将编译后的exe放到服务器上面就无法访问了,一直提示登录失败.而 ...
- 雷林鹏分享:Ruby 类和对象
Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...
- 10X Genomics vs. PacBio
10X Genomics已经广泛应用于单细胞测序.组装领域,现在也是火的不行. 10X Genomics原理 通过将来自相同DNA片段(10-100kb)的reads加上相同的barcode,然后在i ...
- 01-trie练习
这里用递归实现01-trie, 可以看做是区间长度为2的幂的权值线段树, 能实现权值的所有操作, 异或时, 翻转左右儿子即可. 练习1 CF 817E Choosing The Commander 大 ...
- Underground Lab CodeForces - 782E (欧拉序)
大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点 $n$结点树的 ...
- A Creative Cutout CodeForces - 933D (计数)
大意:给定$n$个圆, 圆心均在原点, 第$k$个圆半径为$\sqrt{k}$ 定义一个点的美丽值为所有包含这个点的圆的编号和 定义函数$f(n)$为只有$n$个圆时所有点的贡献,求$\sum_{k= ...
- unity3d 博客
博客: 1.http://my.csdn.net/caoboya 2.http://my.csdn.net/OnafioO
- ADO.NET 体系结构
两个部分 .NET 数据提供者 DataSet 数据提供者 SqlClient 提供者 OleDb 提供者 Odbc 提供者 数据提供者组件 数据对象 DataSet
- <meta http-equiv="X-UA-Compatible" content="IE=7" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />意思是将IE8用IE7进行渲染,使网页在IE ...