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);
}
}
}
}

并查集+路径压缩(poj1988)的更多相关文章

  1. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

  3. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  4. HDOJ 3635 并查集- 路径压缩,带秩合并

    思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...

  5. LA 并查集路径压缩

    题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...

  6. snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)

    1110: 传输网络 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 43  Solved: 18[Submit][Status][Web Board] ...

  7. - > 并查集+路径压缩(详解)(第一节)

    先举一个友爱的例子解释一下并查集: 话说江湖上散落着各式各样的大侠,有上千个之多. 他们没有什么正当职业,整天背着剑在外面走来走去,碰到和自己不是一路人的,就免不了要打一架.但大侠们有一个优点就是讲义 ...

  8. PAT甲级1013题解——并查集+路径压缩

    题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...

  9. HDU 3635 并查集+路径压缩+记录每个点移动次数

    题意: 给定n个点 oper个操作 每个点有1个龙珠 下面2种操作: T u v 把u点所有龙珠搬到v Q u  问u点当前所在城市 u点所在城市有几个龙珠 u点被移动几次 思路: 并查集可以求出 u ...

随机推荐

  1. PCL,VTK及其依赖库的编译-十分详细

    所有库的编译教程都很详细,全都上传到百度文库. 1.VS2013-Qt5.5.1-动态编译-VTK7.0.0http://wenku.baidu.com/view/749528a433687e21ae ...

  2. spark学习系列

    转自: http://www.cnblogs.com/magj2006/p/4316264.html spark 系列文章汇总 源码导读 spark 源码导读1 从spark启动脚本开始 spark ...

  3. nginx报错:No package erlang available

    问题 yum install erlang 报错:No package erlang available 同样的,如果我们在安装nginx的时候出现"No package nginx ava ...

  4. circRNA 序列提取中的难点

    在预测circRNA时,都是检测breakpoint 处的reads 数,最后给出的环状RNA的ID 都是诸如 chr14:106994222-107183708 这样的形式,给出了起始和终止位置: ...

  5. 【Java集合的详细研究6】Java 数组

    Java 语言中提供的数组是用来存储固定大小的同类型元素. 声明数组变量 double[] myList; // 首选的方法 或 double myList[]; // 效果相同,但不是首选方法 创建 ...

  6. 使用 JMeter 完成常用的压力测试 [转]

    讲到测试,人们脑海中首先浮现的就是针对软件正确性的测试,即常说的功能测试.但是软件仅仅只是功能正确是不够的.在实际开发中,还有其它的非功能因素也起着决定性的因素,例如软件的响应速度.影响软件响应速度的 ...

  7. Matlab中imread函数使用报错“不应为MATLAB 表达式”分析

    问题描述: 使用imread读取特定路径下的文件时,会提示出错! >> mytest错误: 文件:mytest.m 行:10 列:87不应为 MATLAB 表达式. 出错行: Images ...

  8. unity 打包apk安装失败

    Unity 打包Apk安装失败,错误提示:安卓未完成. 解决方案:检查BundleID是否一致

  9. mysql 中实现行变列

    前言: mysql行列变化,最难的就是将多个列变成多行,使用的比较多的是统计学中行变列,列变行,没有找到现成的函数或者语句,所以自己写了存储过程,使用动态sql来实现,应用业务场景,用户每个月都有使用 ...

  10. 查看网卡流量:sar

    sar(System Activity Reporter 系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,但我们一般用来监控网卡流量 [roo ...