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). The size of input file does not exceed 5MB.

Output

For each type-3 command, output 2 integers: the number of elements and the sum of elements.

Sample Input

5 7
1 1 2
2 3 4
1 3 5
3 4
2 4 1
3 4
3 3

Output for the Sample Input

3 12
3 7
2 8

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}


Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

ac代码:

#include<stdio.h>
int f[],id[],c[],sum[];
int dex;
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
f[fy]=fx;
c[fx]+=c[fy];
sum[fx]+=sum[fy];
}
}
void del(int x)
{
int fx=find(id[x]);
c[fx]--;
sum[fx]-=x;
id[x]=++dex;
f[dex]=dex;
c[dex]=;
sum[dex]=x; //并查集删除操作
}
int main()
{
int n,q,x,y,z,i;
while(~scanf("%d%d",&n,&q)){
dex=n;
for(i=;i<=n;i++){
f[i]=i;
id[i]=i;
c[i]=;
sum[i]=i;
}
for(i=;i<=q;i++){
scanf("%d",&x);
if(x==){
scanf("%d%d",&y,&z);
join(id[y],id[z]);
}
else if(x==){
scanf("%d%d",&y,&z);
int fy=find(id[y]);
int fz=find(id[z]);
if(fy!=fz){
del(y);
join(id[y],id[z]);
}
}
else{
scanf("%d",&y);
int fy=find(id[y]);
printf("%d %d\n",c[fy],sum[fy]);
}
}
}
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. HDU 2473 Junk-Mail Filter(并查集的删除操作)

    题目地址:pid=2473">HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了 ...

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

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

  4. 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 ...

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

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

  6. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

  7. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  8. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

随机推荐

  1. webstorm 设置IP 访问 手机测试效果

    http://www.cnblogs.com/gulei/p/5126383.html 前端开发中,经常需要将做好的页面给其他同事预览或手机测试,之前一直用的第三方本地服务器usbwebserver, ...

  2. pppoe server 搭建

    Ubuntu 上搭建 pppoe server sudo apt-get install pppoe $ cat /etc/ppp/pppoe-server-options # PPP options ...

  3. teradata培训文档 相关索引

    teradata培训文档 http://wenku.baidu.com/view/ec44c201cc175527072208ba.html Teradata 和Greenplum 的讨论 http: ...

  4. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

  5. Vue入门(一) 环境配置

    Node.js 安装,https://nodejs.org/en/  默认安装就可以 安装好后测试版本,cmd  键入命令 1.node -v 2.npm -v 安装,淘宝 NPM         n ...

  6. python2&python3的区别

    区别1. python3中>>>range<3,6>range<3,6> python2中>>>range<3,6>[3,4,5 ...

  7. MSVC环境,Qt代码包含中文无法通过构建的解决方案

    将代码文件的编码更改为ANSI(方便起见,将Qt Creator的Text Editor默认编码改为System) 这样就可以通过构建,不过会出现中文乱码的问题 还需要使用QStringLiteral ...

  8. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  9. CentOS-7-64bit 下为firefox安装flashplayer

    最近更新了Centos 7 还是有一些不习惯的 给ff安flashplayer插件时,按centos 6.x的方法时都无法成功,后来find了一下,才知道firefox还有一个64bit的文件夹: 解 ...

  10. 盒子的display属性

    <body> <div style="display:inline">Box-1</div> <div style="displ ...