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

题目大意:

有N个立方体和N个格子,1~N编号,一開始i立方体在i号格子上,每一个格子刚好1个立方体。如今m组操作,M a b表示将a号立方体所在的格子的所有立方体放在b号立方体所在的格子的所有立方体上面。C x表示询问x号立方体以下的立方体的个数。

解题思路:

在并查集的基础上。仅仅须要知道x到父亲的距离以及父亲究竟的距离就知道x究竟的距离。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std; const int maxn=31000;
int father[maxn],cnt[maxn],dis[maxn]; int find(int x){
if(father[x]!=x){
int tmp=father[x];
father[x]=find(father[x]);
dis[x]+=dis[tmp];
}
return father[x];
} void combine(int x,int y){
father[x]=y;
dis[x]+=cnt[y];
cnt[y]+=cnt[x];
} int main(){
int m;
scanf("%d",&m);
for(int i=0;i<maxn;i++){
father[i]=i;
cnt[i]=1;
dis[i]=0;
}
while(m-- >0){
char ch;
cin>>ch;
if(ch=='M'){
int a,b;
scanf("%d%d",&a,&b);
if(find(a)!=find(b)) combine(find(a),find(b));
}else{
int x;
scanf("%d",&x);
find(x);
printf("%d\n",dis[x]);
}
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 1988 Cube Stacking (数据结构-并检查集合)的更多相关文章

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

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

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

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

  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 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  6. POJ——T 1988 Cube Stacking

    http://poj.org/problem?id=1988 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 25865   ...

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

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

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

    哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...

  9. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 使用cocos2d 2.1制作一条河游戏(4): 主要的游戏逻辑BaseLayer设计

    前段时间一直忙着.没有时间更新博客.今天,仍然需要一段时间才能实现对游戏的一小部分,最后打动他. BaseLayer.h: #import <GameKit/GameKit.h> #imp ...

  2. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

  3. hdu3873 有约束条件的最短路

    题目大意:美国佬打算入侵火星,火星上有n个城市,有些城市可能受其他城市保护, 如果i城市受j城市保护,那么你必须先攻占j城市才能再攻占i城市,问你攻占城市n的最短时间是多少. 数据解释: 给定t, 表 ...

  4. C# 引用类型与值类型的区别

    //引用类型(使用了class) class SomeRef{public Int32 x;} //值类型(使用了struct) struct SomeVal{public Int32 x;} sta ...

  5. HSQL

    Whenever I connect to HSQLDB from my application deployed on eclipse Juno, it throws an exception as ...

  6. 【HTML+CSS】(1)基本语法

    HTML基金会 <em>他强调标签,<strong>加粗标签 <q>短文本引用.<blockquote>长文本引用,这两个标签会让文字带双引號.   空 ...

  7. Golang 1.3 发布时间。最终找到地方下载。

    golang 1.3 已发布 但golang.org官方网站被封锁不能下载. 最终找到一个镜像站点. http://golang.so/ http://tip.golang.so/ golang中国的 ...

  8. 开源Math.NET基础数学类库使用(17)C#计算矩阵条件数

    原文:[原创]开源Math.NET基础数学类库使用(17)C#计算矩阵条件数                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  9. Lucene40SkipListWriter

    多级跳跃表是保存在tim文件里的. tip是term index,tim是term dictionary.记忆方法是,p是pointer因此是term index. 这个类会保存多个level的las ...

  10. Android自己定义组件系列【5】——高级实践(1)

    在接下来的几篇文章将任老师的博文<您可以下拉PinnedHeaderExpandableListView实现>骤来具体实现.来学习一下大神的代码并记录一下. 原文出处:http://blo ...