Building Block

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5811    Accepted Submission(s): 1790

Problem Description
John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N。Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds of operation:

M X Y : Put the whole pile containing block X up to the pile containing Y. If X and Y are in the same pile, just ignore this command. 
C X : Count the number of blocks under block X

You are request to find out the output for each C operation.

 
Input
The first line contains integer P. Then P lines follow, each of which contain an operation describe above.
 
Output
Output the count for each C operations in one line.
 
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
 
Sample Output
1
0
2
 
Source
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2819 2817 2824 2821 2820 
 
题解:

因为要查询x的下面有多少blocks,所以我们在普通并查集的基础上在维护两个域num[x]和under[x],分别表示x所在堆的大小以及x下面的元素。

在合并的时候,我们分别取x,y的堆的最下面一块,也就是他们的根a,b.a和b相等就不用处理了。如果不相等,那么就让fa[a] = b.而在这之前,我们要维护size和under,所有x原来所在的堆的每个元素的under都要增加num[b],如果全都修改会超时,所以我们之修改under[a],把其它修改放在压缩里面,要查哪一个再更新。同时,为了方便我们只把size存在根上,也就是num[b]+=num[a],num[a] = 0。

在find的时候,我们进行压缩,这时候更新under[x],under[x]+=under[fx]就可以了。

注意:这题一直wa,原因是在刚开始赋值时,应该从0下标开始,然而题目中标号是从1号开始的,不懂。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
using namespace std;
int fa[],under[],num[];
int p;
char ch[];
int findfa(int k)
{
if (fa[k]==k) return k;
int fa1=fa[k]; //找到之前的那个父亲,这个父亲的under值在后面find的过程中也会变,所以只要加之前的父亲节点的under就行了。
fa[k]=findfa(fa[k]);
under[k]+=under[fa1];
return fa[k];
}
void uni(int x,int y)
{
int fx=findfa(x);
int fy=findfa(y);
if (fx!=fy)
{
under[fx]+=num[fy];
num[fy]+=num[fx];
num[fx]=;
fa[fx]=fy;
}
return;
} int main()
{
for(int i=;i<;i++)
{ fa[i]=i; under[i]=; num[i]=;} scanf("%d",&p);
for(;p>;p--)
{
scanf("%s",&ch);
if (ch[]=='M')
{
int x,y;
scanf("%d%d",&x,&y);
uni(x,y);
} else
{
int x;
scanf("%d",&x);
findfa(x);
printf("%d\n",under[x]);
}
} return ;
}

hdu 2818 Building Block(并查集,有点点复杂)的更多相关文章

  1. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  2. hdu 2818 Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  4. hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...

  5. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  6. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  7. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  8. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  9. HDU 4313 Matrix(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...

随机推荐

  1. jvm2

    垃圾回收器的实现: 1.让用户都暂停,不再产生垃圾,就去收集垃圾.新生代用复制算法清理垃圾,老生代用标记整理算法搜集垃圾. 优秀的算法:服务端默认是CMS收集器. %..jvm案例演示 内存: Jco ...

  2. iOS开发之CoreData数据存储

    iOS开发之CoreData数据存储 参考资料:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreD ...

  3. LocalStorage存储JSON对象的问题

    LocalStorage存储JSON对象的问题   localStorage - 没有时间限制的数据存储 1 var arr=[1,2,3]; 2 localStorage.setItem(" ...

  4. Python学习笔记之Python的多重继承和MixIn

    多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat - 蝙蝠: Parr ...

  5. 关于git pull时出现的问题及解决反思

    前因 在前面由于已经git过一次,按照娄老师的要求,代码一个一个commit,所以删掉之前的git仓库(用la查看,使用rm -rf .git删除).但远程origin已经存在,所以执行git rem ...

  6. Net_Prop 之 CTerrorPlayer 属性

    Sub-Class Table (1 Deep): DT_TerrorPlayer Sub-Class Table (2 Deep): DT_CSPlayer Sub-Class Table (3 D ...

  7. mongodb可视化客户端下载

    网站:https://www.mongodbmanager.com/files/ 直接下载:https://www.mongodbmanager.com/files/mongodbmanagerpro ...

  8. web视频资料百度云分享

    韩顺平  struts视频教程(31讲)密码:hsp789 链接:https://pan.baidu.com/s/1kSP4CFt1gpVikRaqQXkFGA 密码:kzvp 韩顺平 spring ...

  9. springboot的controller使用及url参数的获取

    类上加上@RequestMapping其访问的地址就是类上的加上方法上的菜能访问到该方法,例如上图的地址就是/hello/say @RequestMapping(value = "/hell ...

  10. SQL使用CASE 语句

    CASE 语句可以在SELECT 子句和ORDER BY 子句中使用 CASE语句分为两种Case Simple Expression and Case Search Expression Case ...