【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 ...
随机推荐
- php 获取客户端的ip、地理信息、浏览器信息、本地真实ip
转自:http://www.blhere.com/948.html 这是非常实用的php常用类.获取客户端的ip.地理信息.浏览器信息.本地真实ip 1234567891011121314151617 ...
- git 密钥
为什么配置SHH呢?是为了方便我们剪切代码的时间免密码输入,特别方便如何配置呢? 首先安装git: 先到官网下载:官网下载git 然后安装后在桌面任意空白处右击,选择Git Base Here即可如下 ...
- 【Leetcode链表】奇偶链表(328)
题目 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1 ...
- NSString&NSMutableString常用操作梳理
http://www.cocoachina.com/ios/20150724/12722.html 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Object ...
- 手动实现一个form组件
最近研究了一下element-ui,想着手动实现一下里面的form组件,贴个组件里面的代码 <el-form :model="ruleForm" status-icon :r ...
- Java面向对象----String对象的声明和创建
String a="abcd" 相等 String b="abcd" String a=new String("abcd") 不等于 ...
- form表单提交,后台怎么获取select的值?后台直接获取即可,和input方式一样。
form表单提交,后台怎么获取select的值? 后台直接获取即可,和后台获取input的值方式一样. form提交后,后台直接根据select的name获取即可,即getPara("XXX ...
- IO NIO AIO及常用框架概述
概述 nio 同步: 自己亲自出马持银行卡到银行取钱(使用同步IO时,Java自己处理IO读写). 异步: 委托一小弟拿银行卡到银行取钱,然后给你(使用异步IO时,Java将IO读写委托给OS处理,需 ...
- 2018-8-10-win10-uwp-如何在DataTemplate绑定方法
title author date CreateTime categories win10 uwp 如何在DataTemplate绑定方法 lindexi 2018-08-10 19:16:50 +0 ...
- @codeforces - 1205C@ Palindromic Paths
目录 @description@ @solution@ @accepted code@ @details@ @description@ 这是一道交互题. 现在有一个 n*n 的矩阵,每个位置是 0 或 ...