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. hibernate关联映射学习

  2. Centos下 为Firefox安装Flash插件

    方法一: 直接到官网去下载RPM格式的安装包,下载好后直接 用 rpm -ivh XXXX.rpm 来安装即可. 方法二: 到官网下载tar.gz格式的,自己配置安装: #wget http://fp ...

  3. Django admin coercing to Unicode: need string or buffer, tuple found

    见 http://stackoverflow.com/questions/29762306/django-admin-coercing-to-unicode-need-string-or-buffer ...

  4. iOS - Xcode7.3插件实效问题解决方法

    以往Xcode升级插件实效.1.关闭xcode 2.终端获取uid 3.文件中info.plist手动添加uid,现在不用了.在网上找的,特地记录一下,尤其是插件太多,不会麻烦了. 详细操作步骤: 关 ...

  5. vmware的安装

    VMware Workstation是一款功能强大的虚拟机软件,在不影响本机操作系统的情况下,用户可以在虚拟机中同时运行不同版本的操作系统,用于开发.测试以及部署工作,这边小编为大家分享最新版本VMw ...

  6. Nginx环境下http和https可同时访问方法

    给nginx配置SSL证书之后,https可以正常访问,http访问显示400错误,nginx的配置如下: server { listen 80 default backlog=2048; liste ...

  7. cobbler自动安装脚本

    #!/bin/sh #coding=utf8 ################################################################## #将如下IP修改成你 ...

  8. ecshop 的transport.js 与jqueyr冲突

    1111 {insert_scripts files='common.js,global.js,transport.js'} <script type="text/javascript ...

  9. Swift学习一

    // 定义一个类 class AClass { } // 数据转换 var a = Int(4.555) // 可选值 var num: Int? = Int("23k") // ...

  10. Redis学习——环境搭建以及基础命令使用

    0. 前言: 这篇文章旨在对redis环境的搭建以及对redis有个大概的认识. 一.redis搭建: 环境:ubuntu 14 软件包:redis-3.0.3.tar.gz 安装步骤: 1. 首先解 ...