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

Cube Stacking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 19122   Accepted: 6664
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

题意:可以把题目中的意思抽象为栈集合的合并,题目中有两个操作

M 把包含x的栈放在包含y的栈的上面;

C统计包含x的栈中x下面有多少个元素;

分析:此题是并查集路径压缩的典型题目,与前面的种类并查集还有些不同;题目中用到f[],num[],dis[]三个数组,f数组记录i的父节点,num数组记录以i为根的集合有多少个元素,dis记录i到根节点的距离,那么i下面的元素就是x=finde(i);num[x]-dis[i]-1个元素;

程序:

#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define M 30009
#define eps 1e-10
#define inf 1000000000
#define mod 2333333
using namespace std;
int f[M],num[M],dis[M];
int finde(int x)
{
if(x!=f[x])
{
int t=f[x];
f[x]=finde(f[x]);
dis[x]+=dis[t];
}
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
{
f[y]=x;
dis[y]+=num[x];
num[x]+=num[y];
}
}
int main()
{
int n,i,a,b;
char str[3];
while(scanf("%d",&n)!=-1)
{
for(i=1;i<=M+1;i++)
{
f[i]=i;
num[i]=1;
dis[i]=0;
}
while(n--)
{
scanf("%s",str);
if(str[0]=='M')
{
scanf("%d%d",&a,&b);
make(a,b);
}
else
{
scanf("%d",&a);
int x=finde(a);
printf("%d\n",num[x]-dis[a]-1);
}
}
}
}

最新文章

  1. 双主MySQL+keepalived高可用配置
  2. PHPnow在win8下安装失败的解决办法
  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q40-Q44)
  4. windows 下 redis for php 配置
  5. PHP解释器引擎执行流程 - [ PHP内核学习 ]
  6. MapReduce --全排序
  7. [Flex] PopUpButton系列 —— 控制弹出菜单的透明度、可用、可选择状态
  8. javascript插件编写小结
  9. 第九篇、Swift的基本使用
  10. 95秀-ViewPager 使用实例
  11. SLIC superpixel实现分析
  12. winform正在使用dsoframer迅速&amp;quot;Unable to display the inactive document.Click here to reacitive the document.&amp;quot;
  13. NEFUOJ 500 二分法+最大流量
  14. kafka connect 使用说明
  15. 抖音分享和授权(iOS)
  16. Linux:“awk”命令的妙用
  17. nginx ----&gt; 官网about页面(翻译)
  18. C++数组初始化
  19. python 算法面试题
  20. Prolog学习:数独和八皇后问题

热门文章

  1. 如何修复U盘提示被写保护的问题
  2. Asp.Net MVC EasyUI DataGrid查询分页
  3. qlineedit控件获得焦点
  4. QTableWidget的表头颜色设置
  5. bedtools 的安装与使用
  6. Oracle行列转换的思考与总结
  7. 对于MathType中公式与文字错位的问题怎么解决
  8. linux中nmcli命令详解
  9. [java] java 设计模式(2):抽象工厂模式(Abstract Factory)
  10. ASP.Net MVC开发基础学习笔记(7):数据查询页面