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. 只想着一直调用一直爽, 那API凭证泄漏风险如何破?

    如今各家云厂商都通过给用户提供API调用的方式来实现一些自动化编排方面的需求.为了解决调用API过程中的通信加密和身份认证问题,大多数云厂商会使用同一套技术方案—基于非对称密钥算法的鉴权密钥对,这里的 ...

  2. docker 与host互传文件

    docker 的cp命令可以从容器往外复制,也可以从本机复制的容器. docker cp 文件路径 容器id:/容器目录 docker help cp Usage:    docker cp [OPT ...

  3. 【Mysql的那些事】Django数据库配置

    1:安装与配置 1:pip install PyMySQL(或者直接使用Pycharm的setting,点击添加PyMySQL) 2:在Django的工程同名子目录的__init__.py文件中添加如 ...

  4. CSS3摆动动画效果

    效果图:红包在左右摇晃 代码如下: @keyframes upAnimation { 0 % { transform: rotate(0 deg);transition - timing - func ...

  5. Alpha版本第一周作业

    姓名 学号 周前计划安排 每周实际工作记录 自我打分 LTR 61213 1.撰写博客2.分配具体任务并完成个人任务 1.已完成博客撰写2.任务分配完成并继续构思实现方法 95 LHL 61212 完 ...

  6. Vue电商后台管理系统项目第1天-基本环境搭建&登录功能

    基本环境搭建完成 安装npm包:npm -S i vue vue-router axios element-ui 配置Eslint: 打开设置,搜索Eslint拓展,然后将下面代码覆盖进去即可 { , ...

  7. 使用epoll实现简单的服务器

    1. 头文件 #ifndef __TCP_SERVER_H__ #define __TCP_SERVER_H__ #include <unistd.h> #include <stdi ...

  8. oracle函数 BFILENAME(dir,file)

    [功能]函数返回一个空的BFILE位置值指示符,函数用于初始化BFILE变量或者是BFILE列. [参数]dir是一个directory类型的对象,file为一文件名. insert into lob ...

  9. 解决ViewState过于庞大的问题

    这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间.并且它很 小,可以说是磁盘随便转一圈就能同时读取好多ViewState ...

  10. HZOJ 光

    一道大模拟,打的我要吐了. 先说一下60%暴力吧,其实模拟光的路线即可,最好还是把边界设为障碍,这样就不用判边界了.最后输出n*m可以骗到10分. 注意不要把n和m弄混(愁死我了). #include ...