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. Mysql完全卸载(Windows版本)

    (1)控制面板 ---> 程序和功能 ---> 卸载MySQL Installer: (2)删除MySQL软件安装路径下的MySQL目录,默认目录为 C:\Program Files (x ...

  2. Some cool FireMonkey multi-device components

    http://blogs.embarcadero.com/davidi/2014/01/16/43281 There are many available Delphi and C++Builder ...

  3. python-类(1)

    ·类(Class) 笔记: Python是一种面向对象(Object Oriented)的编程语言,类(Class)是Python的核心概念.在Python中,不管是列表.字符串.函数和类都是对象. ...

  4. python 将歌词解析封装成类,要求:提供一个方法(根据时间返回歌词) - 提示:封装两个类:歌词类、歌词管理类

    自己写的 有更好方案的大佬可以讨论一下 import bisectclass Lrc(): def __init__(self, sec, lrc): self.sec = sec self.lrc ...

  5. dns欺骗之ettercap

    ettercap是一个基于ARP地址欺骗方式的网络嗅探工具,主要适用于局域网. ettercap是一款现有流行的网络抓包软件,它利用计算机在局域网内进行通信的ARP协议的缺陷进行攻击,在目标与服务器之 ...

  6. Ajax的跨域请求——JSONP的使用

    一.什么叫跨域 域当然是别的服务器 (说白点就是去别服务器上取东西) 只要协议.域名.端口有任何一个不同,都被当作是不同的域. 总而言之,同源策略规定,浏览器的ajax只能访问跟它的HTML页面同源( ...

  7. 20155214 2016-2017-2 《Java程序设计》第2周学习总结

    20155214 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 Java的基本类型比C多了boolean型和byte型,缺少了long double型,ch ...

  8. 【转载】图说C++对象模型:对象内存布局详解

    原文: 图说C++对象模型:对象内存布局详解 正文 回到顶部 0.前言 文章较长,而且内容相对来说比较枯燥,希望对C++对象的内存布局.虚表指针.虚基类指针等有深入了解的朋友可以慢慢看.本文的结论都在 ...

  9. P3374 【模板】树状数组 1(单点增减,区间求和)

    P3374 [模板]树状数组 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示 ...

  10. 创龙6748开发板加载.out出现a data verification error occurred, file load failed

    1. 需要提前添加GEL文件 2. 找到GEL文件路径 3. 然后再加载.out文件