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 388A - Fox and Box Accumulation
388A - Fox and Box Accumulation 思路: 从小到大贪心模拟. 代码: #include<bits/stdc++.h> using namespace std; ...
- nginx常用命令汇总
nginx基础命令: sudo nginx // 开启nginx服务器 sudo nginx -s reload // 重启nginx服务器 sudo nginx -s stop // 关闭nginx ...
- python模块——random模块(简单验证码实现)
实现一个简单的验证码生成器 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: 验证 ...
- SQL 基础学习(2) Joining 和function , 作业没有做,需要看百宝箱。NOsql的概念
SQL 基础学习(2) Joining 可以同时关联(joining)多张表进行复杂的查询. 相比于用Rails捞出数据再用Ruby进行过滤组合,使用SQL更加高效,节能. 以下是 users has ...
- Confluence 6 的 Crowd 权限
只读(Read Only) 从 Crowd 上获取的用户,用户组和用户组成员信息只具有读取权限,你只能在 Crowd 上对你的配置进行修改.你不能通过你的应用程序管理员界面修改,用户,用户组,用足成员 ...
- POJ 3481 splay模板
最后撸一发splay. 之前用treap撸的,现在splay也找到感觉了,果然不同凡响,两者之间差别与精妙之处各有其精髓! 真心赞一个! POJ平衡树的题目还是比较少,只能挑之前做过的捏一捏.但是收获 ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- java并发编程:线程安全管理类--原子操作类--AtomicReferenceArray<E>
1.类 AtomicReferenceArray<E> public class AtomicReferenceArray<E>extends Objectimplements ...
- 071——VUE中vuex之使用getters计算每一件购物车中商品的总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- win7激活之系统保留分区的设置与隐藏
激活动具:WIN7Activation_1.7 步骤: 1.直接点激活 2.若提示需设置驱动器号: 右键计算机-管理-磁盘管理-系统保留上点右键-更改驱动器号和路径-添加-点确定即可 3.激活win7 ...