Time Limit: 2000MS Memory Limit: 30000K

Total Submissions: 24007 Accepted: 8432

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

【题解】



题目的意思:

给你n个有标号的木块。

一开始全部摆放在地面上。

然后M 1 6的话,就是把地面上的标号1木块叠在6木块上面.

1

6 2 3 4 5

这时1下面有一个木块6所以C1==1;

再M 2 4,即把2号木块叠在4号木块上面

1 2

6 3 4 5

然后M 2 6即把2号木块所在的堆整个放在1号木块所在的堆

2

4

1

6 3 5

这样4号下面就只有两个木块;

做法:

带权并查集。

父亲指向底端的木块。

re[x]表示当前这个木块下面有多少个木块;

cnt[x]表示x个节点下面包括自己有多少个木块;

用带权向量(我也不知道有没有这个名词,但是听起来挺厉害的。)转移的时候。就不用那么麻烦了;

假设x和y的根节点分别为a,b;

则f[a]=b;

表示把a放在b所在堆的上面;

则a下面需要增加的代价就是cnt[b];

加上去就好了

至于a的上面的元素。会在ff函数里面进行路径压缩的时候累加上去;

所以不用担心。

和银河英雄传说那题很像。

还是安利下带权并查集的通解

http://blog.csdn.net/harlow_cheng/article/details/52737486

#include <cstdio>
#include <iostream> using namespace std; const int MAXN = 30000; int f[MAXN+100], re[MAXN+100],cnt[MAXN+100]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} int ff(int x)
{
if (f[x] == x)
return x;
int olfa = f[x];
f[x] = ff(f[x]);
re[x] = re[x] + re[olfa];
return f[x];
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
for (int i = 1; i <= MAXN; i++)
f[i] = i, re[i] = 0,cnt[i] =1;
int p;
input(p);
for (int i = 1; i <= p; i++)
{
char key[5];
scanf("%s", key);
if (key[0] == 'M')
{
int x, y;
input(x); input(y);
int a = ff(x), b = ff(y);
if (a != b)
{
f[a] = b;
re[a] = re[a]+cnt[b];
cnt[b] += cnt[a];
}
}
else
{
int x;
input(x);
ff(x);
printf("%d\n", re[x]);
}
}
return 0;
}

【35.12%】【POJ 1988】Cube Stacking的更多相关文章

  1. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

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

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

  3. js【输入一个日期】返回【当前12个月每月最后一天】

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  4. 【2017.12.05 智能驾驶/汽车电子】转载:如何成为一名无人驾驶工程师 By刘少山

    之前对无人驾驶的理解就是通过刘少山老师的书:第一本无人驾驶技术书 通读之后,对智能驾驶有了一个初步的认识,如感知.决策.控制都涉及哪些领域,有哪些可以利用的技术: 但经过一段时间的实践,发现即使是在我 ...

  5. 【2020.12.03提高组模拟】A组反思

    估计:40+10+0+0=50 实际:40+10+0+0=50 rank40 T1 赛时看到\(n,m\leq9\),我当机立断决定打表,暴力打了几个点之后发现在\(n\ne m\)且\(k\ne0\ ...

  6. 【2020.12.02提高组模拟】A组反思

    55,rk47 T1 赛时先想了\(trie\),想到不一定是前缀,然后就放弃转为打暴力 得分:\(RE22\) 正解是只用判断\(i\)与\(i+1\)的关系,那么只有两种情况,判断一下然后\(dp ...

  7. 【2020.12.02提高组模拟】球员(player)

    题目 题目描述 老师们已经知道学生喜欢睡觉,Soaring是这项记录保持者.他只会在吃饭或玩FIFA20时才会醒来.因此,他经常做关于足球的梦,在他最近的一次梦中,他发现自己成了皇家马德里足球俱乐部的 ...

  8. 【2020.12.01提高组模拟】卡特兰数(catalan)

    题目 题目描述 今天,接触信息学不久的小\(A\)刚刚学习了卡特兰数. 卡特兰数的一个经典定义是,将\(n\)个数依次入栈,合法的出栈序列个数. 小\(A\)觉得这样的情况太平凡了.于是,他给出了\( ...

  9. 【2020.12.01提高组模拟】A组反思

    105,rk45 T1 赛时一开始先打了\(m=0\)的情况,也就是普通的卡特兰数,然后打了暴力,样例过了,把样例改改就不行了,原因没有保证是枚举的是合法的出栈序列 得分:\(WA\&TLE1 ...

随机推荐

  1. Java练习 SDUT-2192_救基友记2

    救基友记2 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 屌丝WP的好基友CZ又被妖鬼给抓走了(CZ啊,CZ-.怎么说 ...

  2. parkingLot

    一个支付宝停车支付生活号前端页面 //index.html //自定义键盘 <!DOCTYPE html> <html> <head> <meta chars ...

  3. 如何在“代码”视图中工作并充分利用 Dreamweaver 的编码功能。如 Emmet 缩写

    可通过多种方式在 Dreamweaver 中处理代码. 您可以使用“新建文档”对话框打开新的代码文件,然后开始键入您的代码. 在 Dreamweaver 中创建新的代码文件 键入时,会显示代码提示以帮 ...

  4. 【Django入坑之路】Form组件

    1:From组件的简单使用 1创建From: #导入模块 from django import forms from django.forms import fields, widgets # 导入自 ...

  5. 基于PyTorch的Seq2Seq翻译模型详细注释介绍(一)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qysh123/article/detai ...

  6. iptables 限制访问规则

    iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT把这条语句插在input链的最前面(第一条),对状态为ESTABLI ...

  7. Refs

    一.The ref callback attribute ref:reference,父组件引用子组件 组件并不是真实的 DOM节点,而是存在于内存之中的一种数据结构,叫做虚拟DOM.只有当它插入文档 ...

  8. laravel 增加不存在数据库的字段

    有时候您可能想要增加不存在数据库字段的属性数据.这时候只要定义一个获取器即可: public function getIsAdminAttribute() { return $this->att ...

  9. oracle中=>是什么意思呢?

    => 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...

  10. vbox ubuntu虚拟机中加载笔记本内置摄像头

    使用C:\Program Files\Oracle\VirtualBox\VBoxManage.exe工具加载摄像头 1,显示可用摄像头 C:\Program Files\Oracle\Virtual ...