http://poj.org/problem?id=1988

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 25865   Accepted: 9044
Case Time Limit: 1000MS

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 Output

1
0
2

Source

 
查询x时,先维护x下边的、
 #include <algorithm>
#include <cstdio> using namespace std; const int N();
int fa[N],sum[N],beh[N]; int find(int x)
{
if(fa[x]==x) return x;
int dad=find(fa[x]);
beh[x]+=beh[fa[x]];
return fa[x]=dad;
}
inline void combine(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
beh[x]+=sum[y];
sum[y]+=sum[x];
fa[x]=y;
} int main()
{
for(int i=;i<N;i++)
fa[i]=i,sum[i]=;
int p,u,v; scanf("%d",&p);
for(char ch;p--;)
{
scanf("\n%c%d",&ch,&u);
if(ch=='M')
{
scanf("%d",&v);
combine(u,v);
}
else find(u),printf("%d\n",beh[u]);
}
return ;
}

POJ——T 1988 Cube Stacking的更多相关文章

  1. POJ 1988 Cube Stacking(并查集+路径压缩)

    题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...

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

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

  3. poj.1988.Cube Stacking(并查集)

    Cube Stacking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submi ...

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

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

  5. HDU 1988 Cube Stacking (数据结构-并检查集合)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18834   Accepted: 6535 Ca ...

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

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

  7. POJ 1988 Cube Stacking (种类并查集)

    题目地址:POJ 1988 这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能立即输出,这种话在合并的时候就要所有的结点值都要算出来,可是经过路径压缩之后,没办法所 ...

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

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

  9. POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】

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

随机推荐

  1. requests 常见方法总结

    请求设置:requests.get/post ( url, data={}, params={}, headers={}, timeout=0.01, files={}   Session()    ...

  2. Laravel核心解读--ENV的加载和读取

    Laravel在启动时会加载项目中的.env文件.对于应用程序运行的环境来说,不同的环境有不同的配置通常是很有用的. 例如,你可能希望在本地使用测试的Mysql数据库而在上线后希望项目能够自动切换到生 ...

  3. 洛谷 P2665 [USACO08FEB]连线游戏Game of Lines

    P2665 [USACO08FEB]连线游戏Game of Lines 题目背景 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜. 题目描述 Farmer John has chall ...

  4. 对于startActivity的使用改进

    传统方式 一直以来,使用startActivity的方式就是例如以下: 比方从AActivity跳转到BActivity.那么我们是在AActivity中这样去写: Intent intent = n ...

  5. Android面试题目整理与解说(一)

    这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的添加,假设答案有错误,希望大家能够指正 1.简述Activity的生命周期 当Activity開始启动的时候,首先调用onCre ...

  6. Linux安装PHP MongoDB扩展

    本文将讲述一下本人安装MongoDB扩展的过程,大家可以略作参考 安装环境 Linux环境:CentOS 6.5 Apache版本:2.4 PHP版本:5.4.3 MongoDB版本:2.6.5 一. ...

  7. BZOJ 1588 平衡树 模板题

    Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int si ...

  8. Eclipse输入智能提示设置

    JAVA智能提示 展开菜单 Window -> preferences -> Java -> Editor -> Content assist 找到右边的Auto-Activa ...

  9. window.location.href=*.html访问没反应问题

    window.location.href=*.html访问没反应问题 1.SpringMvc配置中:扫描controller.视图层配置路径是否正确 报doc根路径 <mapper>匹配错 ...

  10. kindeditor 不能编辑 问题

    /*显示上传窗体*/ function ShowUplodToDaily() { var _sdata = grid.getSelecteds(); if (_sdata) { /*创建编辑器*/ v ...