Cube Stacking

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 21157   Accepted: 7395
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

 #include<stdio.h>
#define N 30001 int count[N], num[N], pre[N]; void inite()
{
for(int i = ; i < N; i++)
{
count[i] = ;
num[i] = ;
pre[i] = i;
}
} int find(int x)
{
if(pre[x] == x)
return x; int t = find(pre[x]);
count[x] += count[pre[x]];
pre[x] = t;
return t; }
void Union(int x, int y)
{
int i = find(x);
int j = find(y);
if(i == j)
{
return;
}
count[i] = num[j];
num[j] += num[i];
pre[i] = j;
} int main()
{
int i, x, y, n;
char s[];
scanf("%d",&n);
inite();
for(i = ; i < n; i++)
{
scanf("%s",s);
if(s[] == 'M')
{
scanf("%d%d",&x,&y);
Union(x,y);
}
else if(s[] == 'C')
{
scanf("%d",&x);
int c = find(x);
printf("%d\n",count[x]);
}
}
return ;
}
 

POJ1988 并查集的使用的更多相关文章

  1. poj1988(并查集)

    题目链接:http://poj.org/problem?id=1988 题意:有n个箱子,初始时每个箱子单独为一列: 接下来有p行输入,M, x, y 或者 C, x: 对于M,x,y:表示将x箱子所 ...

  2. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  3. POJ1988 Cube Stacking 【并查集】

    题目链接:http://poj.org/problem?id=1988 这题是教练在ACM算法课上讲的一道题,当时有地方没想明白,现在彻底弄懂了. 题目大意:n代表有n个石头,M a, b代表将a石头 ...

  4. poj1988 Cube Stacking 带权并查集

    题目链接:http://poj.org/problem?id=1988 题意:有n个方块,编号为1-n,现在存在两种操作: M  i  j  将编号为i的方块所在的那一堆方块移到编号为j的方块所在的那 ...

  5. bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...

  6. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  7. 并查集——poj1988(带权并查集中等)

    一.题目回顾 题目链接:Cube Stacking 题意:有n个箱子,初始时每个箱子单独为一列:接下来有p行输入,M, x, y 或者 C, x: 对于M,x,y:表示将x箱子所在的一列箱子搬到y所在 ...

  8. POJ1988(Cube Stacking)--并查集

    题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问 ...

  9. poj1988 Cube Stacking(并查集

    题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[ ...

随机推荐

  1. 【PAT L2-001】最短路计数

    给定一个无向带权网络,无负边,无重边和自环,每个顶点有一个正数权值.首先求特定原点s到终点d的最短路的个数:然后求所有最短路中顶点权值a[i]之和最大的那条,输出这条路径. 可用dijkstra算法求 ...

  2. MySQL 索引优化全攻略

    所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找.而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...

  3. 十分钟搭建自己的hadoop2/CDH4集群

    版本及准备 我部署的是hadoop-2.0.0-cdh4.2.0.tar.gz,下载地址为http://archive.cloudera.com/cdh4/cdh/4/hadoop-2.0.0-cdh ...

  4. 再谈cacheAsBitmap

    cacheAsBitmap这个属性很多人都知道,但少有人明白它到底是如何生效的.虽然看名字是转换为位图处理,但用起来的时候感觉却也不过如此.所以,不少人最终选择自己转换Bitmap. 当然,自己转Bi ...

  5. servlet核心API的UML图

  6. Gstreamer的一些基本概念与A/V同步分析

    一.媒体流(streams )流线程中包含事件和缓存如下:-events     -NEW_SEGMENT    (NS)     -EOS                (EOS)  *     - ...

  7. 伪元素first-letter(首字母变大)

    让首字母变大 <p>Do you like to ride a bicycle?</p> p:first-letter{ font-size: 34px; }

  8. SQL Server -SET NOCOUNT

    SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 语法 SET NOCOUNT { ON | OFF } 注释 当 SET NOCOUNT 为 ON ...

  9. Ext JS中Button的一般使用

    Ext JS中Button按钮的显示,以及按钮的部分事件 一.属性 renderTo:将当前对象所生成的HTML对象存放在指定的对象中 text:得到按钮名称 minWidth:按钮最小宽度 hidd ...

  10. ElasticSearch入门知识扫盲

    ElasticSearch 入门介绍 tags: 第三方 lucene [toc] 1. what Elastic Search(ES)是什么 全文检索和lucene 全文检索 优点:高效,准确,分词 ...