Cube Stacking
Cube Stacking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 21350 Accepted: 7470
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
USACO 2004 U S Open
并查集的合并,每个点记录到栈底的距离,栈底的元素记录栈的大小方便合并
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const int MAX = 31000;
const int INF = 0x3f3f3f3f;
struct node
{
int pre;
int dis;
int Size;
}a[MAX];
int m;
int Find(int x)
{
if(a[x].pre==x)
{
return x;
}
int t=a[x].pre;
a[x].pre=Find(t);
a[x].dis+=a[t].dis;//将距离更新
return a[x].pre;
}
void Join(int x,int y)
{
int b=Find(x);
int c=Find(y);
a[b].pre=c;//将两个栈的栈底相连
a[b].dis+=a[c].Size;//上面的栈底更新距离
a[c].Size+=a[b].Size;//下面的栈底更新大小
a[b].Size=0;//不在为栈底,大小为零
}
int main()
{
scanf("%d",&m);
char s[5];
int u,v;
for(int i=0;i<MAX;i++)
{
a[i].dis=0;
a[i].Size=1;
a[i].pre=i;
}
for(int i=0;i<m;i++)
{
scanf("%s",s);
if(s[0]=='M')
{
scanf("%d %d",&u,&v);
Join(u,v);
}
else
{
scanf("%d",&u);
Find(u);//要先更新一遍
printf("%d\n",a[u].dis);
}
}
return 0;
}
Cube Stacking的更多相关文章
- poj.1988.Cube Stacking(并查集)
Cube Stacking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submi ...
- POJ 1988 Cube Stacking(带权并查集)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 23678 Accepted: 8299 Ca ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- Cube Stacking(并差集深度+结点个数)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21567 Accepted: 7554 Ca ...
- HDU 1988 Cube Stacking (数据结构-并检查集合)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18834 Accepted: 6535 Ca ...
- 加权并查集(银河英雄传说,Cube Stacking)
洛谷P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.宇宙历七九九年,银河系的两大军事集团在 ...
- POJ 1988 Cube Stacking(并查集+路径压缩)
题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...
- [Usaco2004 Open]Cube Stacking 方块游戏
题面: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000)个指令.指令有两种 ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
随机推荐
- Swift游戏实战-跑酷熊猫(一) 简介 (含源代码)
优酷观看地址:http://v.youku.com/v_show/id_XNzM2Nzc2MTIw.html 通过这个小游戏,我们能够接触到物理系统(SKPhysicsBody,physicsWorl ...
- zabbix添加邮件报警机制
zabbix添加邮件报警机制 作者:尹正杰 还记得之前跟大家聊过的一个如何监控一个目录的话题吗?我们虽然监控出来数据了,也有数据了,但是,只是监控也没有用啊~因为我们不能24小时盯着屏幕然后 出了事情 ...
- vs2010打包系统必备选择.net framework 3.5sp1编译错误的解决方法
利用visual studio 2010进行打包程序,默认安装的是Framework 4.0,如果需要将3.5sp1打包到系统中一起安装(选择了"从与我的应用程序相同的位置下载系统必备组件& ...
- android开发之如何使TabHost的TabWidget位于屏幕下方
更改TabHost里的第一个LinearLayout为RelativeLayout.并在TabWidget中添加android:layout_alignParentBottom="true& ...
- server端推送消息机制
推送技术相关请参加WIKI: https://zh.wikipedia.org/wiki/%E6%8E%A8%E9%80%81%E6%8A%80%E6%9C%AF 场景: 监控系统:后台硬件温度.电压 ...
- PAT乙级 1020. 月饼 (25)(只得到23分)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- 使用Graham扫描法求二维凸包的一个程序
#include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...
- Sinatra+SQLite3+DataMapper - 十分完整的tutorial - “Superdo”
原文地址:https://ididitmyway.herokuapp.com/past/2010/3/30/superdo_a_sinatra_and_datamapper_to_do_list/ 这 ...
- 【GDI+】 线段 文字 定位的问题(二)
继续: 经过上文的分析,似乎可以得到类似这样的想法: 由此 分为左右两侧进行区分绘制,应该就可以获得想要的结果了~
- owner window 和 parent window 有什么区别?
1.Pop-up窗口: 一个弹出窗口是必须具有WS_POPUP属性的窗口,弹出窗口只能是一个Top-Level窗口,不能是子窗口,弹出窗口多用于对话框和消 ...