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. [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)

    转自:http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=981 http://www.cnblogs.com/ivanyb/archi ...

  2. 【android】开发笔记系列UI篇

    弹出View添加阴影效果 系统自带就有,在android studio上直接写入背景颜色 android:background="@android:drawable/dialog_holo_ ...

  3. Django 部署(Apache下)

    前言: 因为需要在服务器下运行python脚本,所以需要搭建Django服务器.所以将自己的学习过程也记录下来,方便日后查阅. 本文环境如下: Ubuntu 16.04  python2.7 Apac ...

  4. Pyspark访问Hbase

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7347167.html 转载请注明出处 记录自己最近抽空折腾虚拟机环境时用spark2.0的pyspark ...

  5. Table的一些设置(自适应以及溢出)

    table的两个属性 单行溢出点点显示 表格的宽度设置 双栏自适应连续连续英文符换行 1.table重置的两个属性: ①border-collapse: collapse;      /* 为表格设置 ...

  6. office使用技巧

    一.excel 1.在空格内换行:ALT+ENTER 2.打出勾:插入->符号

  7. 【转载】JExcelApi(JXL)学习笔记

    在公司的项目中,有excel生成.导出的需求,因此学习了用JXL读写excel,做个简单的笔记,以供参考.      实现用java操作excel的工具,一般用的有两个:一个是JXL,另一个是apac ...

  8. hdu5727

    Necklace SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX ...

  9. Windows下tomcat进程监控批处理程序

    在Windows下tomcat进程监控批处理程序脚本如下: @echo off ::tomcat安装目录 set _tomcatDir=E:\myFiles\apache-tomcat-8.5.31 ...

  10. Java 数据结构之双向链表

    一.概述: 1.什么是双向链表: 链表中的每个节点即指向前面一个节点,也指向后面一个节点,就像丢手绢游戏一样,每个人都手拉手 2.从头部插入 要对链表进行判断,如果为空则设置尾节点为新添加的节点,如果 ...