【35.12%】【POJ 1988】Cube Stacking
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的更多相关文章
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- js【输入一个日期】返回【当前12个月每月最后一天】
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...
- 【2017.12.05 智能驾驶/汽车电子】转载:如何成为一名无人驾驶工程师 By刘少山
之前对无人驾驶的理解就是通过刘少山老师的书:第一本无人驾驶技术书 通读之后,对智能驾驶有了一个初步的认识,如感知.决策.控制都涉及哪些领域,有哪些可以利用的技术: 但经过一段时间的实践,发现即使是在我 ...
- 【2020.12.03提高组模拟】A组反思
估计:40+10+0+0=50 实际:40+10+0+0=50 rank40 T1 赛时看到\(n,m\leq9\),我当机立断决定打表,暴力打了几个点之后发现在\(n\ne m\)且\(k\ne0\ ...
- 【2020.12.02提高组模拟】A组反思
55,rk47 T1 赛时先想了\(trie\),想到不一定是前缀,然后就放弃转为打暴力 得分:\(RE22\) 正解是只用判断\(i\)与\(i+1\)的关系,那么只有两种情况,判断一下然后\(dp ...
- 【2020.12.02提高组模拟】球员(player)
题目 题目描述 老师们已经知道学生喜欢睡觉,Soaring是这项记录保持者.他只会在吃饭或玩FIFA20时才会醒来.因此,他经常做关于足球的梦,在他最近的一次梦中,他发现自己成了皇家马德里足球俱乐部的 ...
- 【2020.12.01提高组模拟】卡特兰数(catalan)
题目 题目描述 今天,接触信息学不久的小\(A\)刚刚学习了卡特兰数. 卡特兰数的一个经典定义是,将\(n\)个数依次入栈,合法的出栈序列个数. 小\(A\)觉得这样的情况太平凡了.于是,他给出了\( ...
- 【2020.12.01提高组模拟】A组反思
105,rk45 T1 赛时一开始先打了\(m=0\)的情况,也就是普通的卡特兰数,然后打了暴力,样例过了,把样例改改就不行了,原因没有保证是枚举的是合法的出栈序列 得分:\(WA\&TLE1 ...
随机推荐
- oralce update操作
1.基本语法:update 表名 set 列名=表达式 [列名=表达式. . . ] where 条件 2.使用的注意事项: v UPDATE语法可以用新值更新原有表行中的各列 把zs的性别改为女 ...
- HDU_1021:Fibonacci Again
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- Java练习 SDUT-1184_拍皮球
C语言实验--拍皮球 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小瑜3岁了,很喜欢玩皮球,看来今后喜欢打篮球的^_ ...
- javascript实现html中关键字查询
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 04使用harbor配置私仓
安装harbor之前,需要安装好Python,Docker,DockerCompose.Python需要2.7以上的版本,Docker需要1.10以上的版本:Docker Compose 需要1.6. ...
- Redis源码解析:09redis数据库实现(键值对操作、键超时功能、键空间通知)
本章对Redis服务器的数据库实现进行介绍,说明Redis数据库相关操作的实现,包括数据库中键值对的添加.删除.查看.更新等操作的实现:客户端切换数据库的实现:键超时相关功能的实现.键空间事件通知等. ...
- 2019-8-31-dotnet-通过-WMI-拿到显卡信息
title author date CreateTime categories dotnet 通过 WMI 拿到显卡信息 lindexi 2019-08-31 16:55:58 +0800 2019- ...
- HDFS Concepts-blocks
- CF1054F Electric Scheme
CF1054F Electric Scheme 其实没啥的. 离散化后,每行每列选择一个. 但是可能会相交 每行或每列相邻两个点成为一小段. 小段按照行列左右部点 小段有交,连inf边,每个s-左, ...