poj.1988.Cube Stacking(并查集)
Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
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
* 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
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(并查集)的更多相关文章
- POJ 1988 Cube Stacking(并查集+路径压缩)
题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- [POJ 1988] Cube Stacking (带值的并查集)
题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...
- POJ 1988 Cube Stacking(带权并查集)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 23678 Accepted: 8299 Ca ...
- POJ 1988 Cube Stacking 【带权并查集】
<题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...
- POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】
Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...
- POJ 1988 Cube Stacking(带权并查集)
哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...
- POJ 1988 Cube Stacking (种类并查集)
题目地址:POJ 1988 这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能立即输出,这种话在合并的时候就要所有的结点值都要算出来,可是经过路径压缩之后,没办法所 ...
- POJ1988(Cube Stacking)--并查集
题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问 ...
随机推荐
- ExceptionLess新玩法 -- 审计日志
审计日志 这算是一个挺酷的功能,把每个请求都记录下来,之前在abp中看到过这个功能,配合可视化的界面,简直是在装逼 看到了exceptionless后,心念一动,我也可以根据它做一个审计日志的功能.这 ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- python04 面向对象编程02
为啥要用类而不用函数呢 记住两个原则: 减少重复代码 代码会经常变更 2 会对变量或字符串的合法性检测(在实例初始化的时候能够统一初始化各个实例的变量,换做函数来说,要弄出同样的变量那么在初始化 ...
- Notepad++ 开启「切分窗口」同时检视、比对两份文件
Notepad++ 是个相当好用的免费纯文本编辑器,除了内建的功能相当多之外,也支持外挂模块的方式扩充各方面的应用.以前我都用 UltraEdit 跟 Emeditor,后来都改用免费的 Notepa ...
- ClassLoader 详解及用途(写的不错)
ClassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回 这个类的class对象. 1.1 几个相关概念Clas ...
- char *p = "abcdefg"; p[0] = p[1]出错
参考:http://blog.sina.com.cn/s/blog_5c0172280100ut4o.html 1.char *s="abc"; 看这个赋值: 右边,是" ...
- python数据库操作常用功能使用详解(创建表/插入数据/获取数据)
实例1.取得MYSQL版本 复制代码 代码如下: # -*- coding: UTF-8 -*-#安装MYSQL DB for pythonimport MySQLdb as mdbcon = Non ...
- 入门:PHP:hello world!
<?php echo 'hello'."\n"." world!"."good night!";//2016.09.18 22:57? ...
- macbook pro的usb串口失效的的处理方法
macbook pro的usb串口失效的的处理方法 2011-08-24 12:14:32| 分类: mac|举报|字号 订阅 今天开电脑,无端端一个usb的串口失效了,接入鼠标 iphon ...
- PHP站内搜索、多关键字、加亮显示
php搜索代码: 搜索以PHP100开头: SELECT * FROM teble WHERE title LIKE 'PHP100%' 搜索以PHP100结束: SELECT * FROM te ...