Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:  moves and counts.  * In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.   * In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value. 
Write a program that can verify the results of the game. 

Input

* Line 1: A single integer, P 
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X. 
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself. 

Output

Print the output from each of the count operations in the same order as the input file. 

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Outpu1

0
2

题目意思:有n块方块,p次操作,两种类型,'M a b' 意思是将含有方块a的堆挪到b上:‘C a’意思是查询方块a下面有几个方块。
解题思路:这道题要使用并查集来进行计算。首先为什么会考虑使用并查集呢?因为每一次操作都是把含有a的堆挪到b上,这个时候将会把b包含到这个堆里面,重新组合成一个新的堆,这是什么?状态划分。这道题的难点主要在于虽然在了同一个集合中,但要询问这个集合内的关系,查询a下面几个方块,就可以看成其在集合中的深度。sum[n]数组来统计当前堆n中方块个数。under[n]数组来统计当前n下面的方块个数。在进行计算的时候,路径压缩和合并均更新under的值。未进行路径压缩的时候,方块n所记录的under并非final value, 仅仅只是包含了到root[n]的方块个数。 通过路径压缩的过程,不断的增加当前n的根节点(递归)的under值,求出最终的under值。进行路径合并的时候,更新sum值和under值。当一个堆放在另一个堆上时,被移动的堆的under值一定为0, 此时更新under值为另一个堆的根的sum值,即计算出了此处的部分under值。然后更新合并堆的sum值。

 #include<cstdio>
#include<cstring>
#define maxs 30010
using namespace std;
int pre[maxs];
int sum[maxs];
int under[maxs];
void init()
{
int i;
for(i=; i<maxs; i++)
{
pre[i]=i;
under[i]=;
sum[i]=;
}
}
int Find(int x)
{
int t;
if(x==pre[x])
{
return x;
}
t=Find(pre[x]);
under[x]+=under[pre[x]];
pre[x]=t;
return pre[x];
}
void Union(int x,int y)
{
int a=Find(x);
int b=Find(y);
if(a==b)
{
return ;
}
pre[a]=b;
under[a]=sum[b];///将a堆放在b堆之上,更新此时a堆下面的数量
sum[b]+=sum[a];///将a堆和b堆合为一个整体,更新整体新堆的数量
} int main()
{
int n;
char q;
int a,b;
scanf("%d",&n);
getchar();
init();
while(n--)
{
scanf("%c",&q);
if(q=='M')
{
scanf("%d%d",&a,&b);
getchar();
Union(a,b);
}
else if(q=='C')
{
scanf("%d",&a);
getchar();
b=Find(a);
printf("%d\n",under[a]);
}
}
return ;
}

 

Cube Stacking P0J 1988(加权并查集)的更多相关文章

  1. POJ 1988 Cube Stacking( 带权并查集 )*

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

  2. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

  3. [POJ 1988] Cube Stacking (带值的并查集)

    题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...

  4. POJ 1988 Cube Stacking(带权并查集)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 23678   Accepted: 8299 Ca ...

  5. POJ 1988 Cube Stacking 【带权并查集】

    <题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...

  6. poj 1988 Cube Stacking【带权并查集】

    设s[x]为x所在栈里的个数,c[x]表示x下面有几个,合并的时候直接合并s,然后路径压缩的时候更新c即可 #include<iostream> #include<cstdio> ...

  7. hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14

    题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...

  8. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  9. HDU 3407.Zjnu Stadium 加权并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. centos7 安装mysql5.7以及一些细节问题

    突然发现我的新服务器上没有mysql,所以想安装一个,上次在我的window电脑上安装MySQL8.0我真的要气死了,和5.7修改密码的方式不一样,弄了很久,所以我决定还是不用安装8.0了,5.7就可 ...

  2. 在UIWindow上加类似于“回到顶部”的按钮

    在公司上个版本的开发中遇到了一个UI布局的小问题: 某个页面需要增加一个分享按钮,但是该页面是二级页面,导航栏右边也已经放置了2个button. 起初和老大谈论这个问题的时候想到的方法是导航栏右边加三 ...

  3. Shellinabox on centos6.9

    介绍 一款实用的web linux终端, 并且保证操作安全性(屏蔽root用户) 下面以centos6.9为例 安装 首先安装epel仓库,再安装shellinabox yum -y install ...

  4. Home Assistant系列--之树莓派安装Samba 和 Jupyter Notebook

    1.什么是Samba? Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上 ...

  5. 收藏Linux命令

    http://www.cnblogs.com/laov/p/3541414.html#zhiling 1.压缩文件夹 http://www.cnblogs.com/eoiioe/archive/200 ...

  6. 自定义udf添加一列

    //创建得分窗口字典 var dict= new mutable.HashMap[Double, Int]() ){ dict.put(result_Score(i),i) } //自定义Udf函数 ...

  7. 将linux上的Java代码上传到码云

    将linux上的Java代码上传到码云 1.在linux上直接输入命令获取git sudo apt-get install git 显示资源被占用,按照图中方法强制安装 2.建立与教材配套的目录结构 ...

  8. # 20155224 2016-2017-2 《Java程序设计》第10周学习总结

    20155224 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 密码学: 主要是研究保密通信和信息保密的学科, 包括信息保密传输和信息加密存储等. 密码学 ...

  9. 20155320 2016-2017-2《Java程序设计》第十周学习总结

    20155320 2016-2017-2<Java程序设计>第十周学习总结 教材学习内容总结 本周学习目标 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Jav ...

  10. 我与虚拟机的初次接触及初探Liux命令 20155338

    初识虚拟机及学习Linux命令的些许收获 虚拟机的安装 这个假期算是第一次正式的接触了虚拟机,以前在平时生活中也有听到过,但是真正自己动手安装虚拟机却是第一次,确实是既紧张又兴奋. 我是依据老师所发的 ...