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的更多相关文章

  1. poj.1988.Cube Stacking(并查集)

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

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

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

  3. 【POJ 1988】 Cube Stacking (带权并查集)

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

  4. Cube Stacking(并差集深度+结点个数)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 21567   Accepted: 7554 Ca ...

  5. HDU 1988 Cube Stacking (数据结构-并检查集合)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18834   Accepted: 6535 Ca ...

  6. 加权并查集(银河英雄传说,Cube Stacking)

    洛谷P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.宇宙历七九九年,银河系的两大军事集团在 ...

  7. POJ 1988 Cube Stacking(并查集+路径压缩)

    题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...

  8. [Usaco2004 Open]Cube Stacking 方块游戏

    题面:     约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱.    游戏开始后,约翰会给贝茜发出P(1≤P≤100000)个指令.指令有两种 ...

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

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

随机推荐

  1. SqlServer2008根据现有表,获取该表的分区创建脚本

    *============================================================== 名称: [GetMSSQLTableScript] 功能: 获取cust ...

  2. String类型方法

    String类型 //1.返回长度 length var a="lynn_hello"; console.log(a.length); //2.相加 concat() 返回一个新的 ...

  3. 解决 Xamarin 拖拽Plain Text 于Layout上时 出现 “The layout could not be loaded:java.lang.System.arraycopy([CI[CII)V” 错误

    右键项目属性

  4. struts不同session范围添加、访问属性

    第01步:编写bean类 package com.self.bean; import java.util.Date; public class User { private Date birthday ...

  5. struts拦截器

    struts中的拦截器相当于过滤器的作用 一在struts.xml中配置拦截器或拦截器栈 <interceptors>!--全部的拦截器 <interceptor name=&quo ...

  6. 雅虎工程师提供的CSS初始化代码

    body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...

  7. FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具(转)

    本文转自:http://blog.csdn.net/djun100/article/details/24237371 这是关于FastJson的一个使用Demo,在Java环境下验证的 class U ...

  8. Mongodb 笔记09 备份、部署MongoDB

    备份 1. 只有在有信心能在紧急情况下完成迅速部署的情况下,备份才是有用的.所以,无论选择了哪种备份技术,一定要对备份及恢复备份的操作进行练习,知道了然于心. 2. 通常情况下,应对副本集的非主节点( ...

  9. python字典和列表使用的要点

    dicts = {} lists = [] dicts['name'] = 'zhangsan' lists.append(dicts) 这时候lists的内容应该是[{'name': 'zhangs ...

  10. ES6,ES2105核心功能一览,js新特性详解

    ES6,ES2105核心功能一览,js新特性详解 过去几年 JavaScript 发生了很大的变化.ES6(ECMAScript 6.ES2105)是 JavaScript 语言的新标准,2015 年 ...