Cube Stacking

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

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>
#include<string.h>
const int M = 3e4 + ;
int f[M] , up[M] , tot[M] ;
int p ;
int s[] ;
int u , v ; int find (int u)
{
if (f[u] == u) return f[u] ;
int t = f[u] ;
f[u] = find (f[u]) ;
up[u] += up[t] ;
return f[u] ;
} void Union (int u , int v)
{
int _u = find (u) , _v = find (v) ;
if (_u != _v) {
f[_v] = _u ;
up[_v] += tot[_u] ;
tot[_u] += tot[_v] ;
}
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
for (int i = ; i < M ; i ++) {
f[i] = i ;
up[i] = ;
tot[i] = ;
}
scanf ("%d" , &p) ;
while (p --) {
scanf ("%s" , s) ;
if(s[] == 'M') {
scanf ("%d%d" , &u , &v) ;
Union (u , v) ;
}
else if (s[] == 'C') {
scanf ("%d" , &u) ;
int _u = find (u) ;
printf ("%d\n" , tot[_u] - up[u] - ) ;
}
}
return ;
}

一些情况:

 

poj.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 (带值的并查集)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. codevs 1229 数字游戏(可重集的全排列)

    传送门 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间.  这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后 ...

  2. ApkTool反编译apk,去除广告或者汉化后重新打包apk,并签名

    http://my.oschina.net/mopidick/blog/135581 以ubuntu的环境为例子,window的大同小异 首先下载apktool工具:https://code.goog ...

  3. UIViewController 的 presentedViewController 和 presentingViewController

    #import "TestViewController.h" #import "OneViewController.h" 在TextViewController ...

  4. JQuery 和JavaScript的区别

    Google提供的jquery包: http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js jQuery官方的jquery包: ...

  5. Linux 常用命令笔记 (持续更新)

    声明:本文是转载前辈的,地址:http://www.cnblogs.com/tovep/articles/2473147.html 在tomcat的bin目录下执行 ./shutdown.sh 为了查 ...

  6. win7安装virtualbox

    1.下载软件 VirtualBox-4.3.24-98716-Win.1425444683.exe 2.修改安装路径 3.确定选择下一步 4.下一步 5.yes 6.安装 7.安装完成 到此win7 ...

  7. Socket通信的理解

    1.Socket(套接字) 是支持TCP/IP通信的基本操作单元.包含通信的五种必须信息:通信使用的协议,本机IP和端口,远程IP和端口. 2. 1.TCP连接 手机能够使用联网功能是因为手机底层实现 ...

  8. 自然语言15_Part of Speech Tagging with NLTK

    https://www.pythonprogramming.net/part-of-speech-tagging-nltk-tutorial/?completed=/stemming-nltk-tut ...

  9. JavaScript的闭包原理

    什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 个人的理解是 ...

  10. Java——字符集:Charset