题目大意:

以N ( 1 ≤ N ≤ 30,000 )个堆栈开始,每个堆栈包含一个单独的立方体。执行P(1≤ P ≤100,000)的操作。

有两种类型的操作:移动和计数。

*在移动操作中,将 包含方块X的堆栈 移动到 包含方块Y的堆栈 顶部。

*在计数操作中,在 包含立方体X的堆栈中 计算立方体X之上的立方体数量并报告该值。

编写一个可以验证游戏结果的程序。

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.

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

#include <bits/stdc++.h>
using namespace std;
int root[],cnt[],dis[];
int get(int n)
{
if(root[n]==n) return n;
int temp=root[n];
root[n]=get(root[n]); ///递归的同时路径压缩 否则cnt连加时会重复
cnt[n]+=cnt[temp]; ///用cnt记录高度
return root[n];
}
void mope()
{
int m,n;
scanf("%d%d",&m,&n);
int gm=get(m),gn=get(n);
if(gm==gn) return;
root[gm]=gn;
cnt[gm]=dis[gn]; ///用dis记录根的高度
dis[gn]+=dis[gm];
dis[gm]=;
}
int main()
{
for(int i=;i<;i++)
{
root[i]=i;
cnt[i]=;
dis[i]=;
}
int p; scanf("%ld",&p);
while(p--)
{
getchar();
char ope;
scanf("%c",&ope);
if(ope=='M') mope();
else if(ope=='C')
{
int n; scanf("%ld",&n);
get(n); //调用get()更新一下cnt的值
printf("%ld\n",cnt[n]);
}
} return ;
}

还没找出为什么用递归方式实现的并查集可以AC

而下面非递归的方式就过不了

#include <bits/stdc++.h>
using namespace std;
int root[],cnt[],dis[];
int get(int n)
{
int dal=n;
while(root[dal]!=dal)
{
cnt[dal]+=cnt[root[dal]];
dal=root[dal];
} int t,odal=n;
while(root[odal]!=dal)
{
t=root[odal];
root[odal]=dal;
odal=t;
}
return dal;
}
void mope()
{
int m,n;
scanf("%d%d",&m,&n);
int gm=get(m),gn=get(n);
if(gm==gn) return;
root[gm]=gn;
cnt[gm]=dis[gn];
dis[gn]+=dis[gm];
dis[gm]=;
}
int main()
{
for(int i=;i<;i++)
{
root[i]=i;
cnt[i]=;
dis[i]=;
}
int p; scanf("%d",&p);
while(p--)
{
getchar();
char ope;
scanf("%c",&ope);
if(ope=='M') mope();
else if(ope=='C')
{
int n; scanf("%d",&n);
get(n);
printf("%d\n",cnt[n]);
}
} return ;
}

USACO2004 cube stacking /// 带权并查集 oj1302的更多相关文章

  1. poj1988 Cube Stacking 带权并查集

    题目链接:http://poj.org/problem?id=1988 题意:有n个方块,编号为1-n,现在存在两种操作: M  i  j  将编号为i的方块所在的那一堆方块移到编号为j的方块所在的那 ...

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

    哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...

  3. bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...

  4. 【BZOJ 3376】[Usaco2004 Open]Cube Stacking 方块游戏 带权并查集

    这道题一开始以为是平衡树结果发现复杂度过不去,然后发现我们一直合并而且只是记录到最低的距离,那么就是带权并查集了,带权并查集的权一般是到根的距离,因为不算根要好打,不过还有一些其他的,具体的具体打. ...

  5. 初涉「带权并查集」&&bzoj3376: [Usaco2004 Open]Cube Stacking 方块游戏

    算是挺基础的东西 Description     约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱.    游戏开始后,约翰会给贝茜发出P(1≤P ...

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

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

  7. POJ 1988 Cube Stacking 【带权并查集】

    <题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...

  8. 洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)

    题目描述 约翰和贝茜在玩一个方块游戏.编号为 1\ldots n 1-n 的 n n ( 1 \leq n \leq 30000 1≤n≤30000 )个方块正放在地上,每个构成一个立方柱. 游戏开始 ...

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

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

随机推荐

  1. java基础集合底层介绍

    ArrayList.Vector.HashMap.HashTable.HashSet的默认初始容量.加载因子.扩容增量 这里要讨论这些常用的默认初始容量和扩容的原因是: 当底层实现涉及到扩容时,容器或 ...

  2. Feign 系列(02)Why Feign

    Feign 系列(02)Why Feign [toc] 1. 什么是 Feign Feign 的英文表意为"假装,伪装,变形", 是一个 Http 请求调用的轻量级框架,可以以 J ...

  3. JUC源码分析-集合篇:并发类容器介绍

    JUC源码分析-集合篇:并发类容器介绍 同步类容器是 线程安全 的,如 Vector.HashTable 等容器的同步功能都是由 Collections.synchronizedMap 等工厂方法去创 ...

  4. [已解决]报错:execjs._exceptions.ProgramError: ReferenceError: window is not defined

    问题: execjs._exceptions.ProgramError: ReferenceError: window is not defined 解决: 定义一个就行 var window = { ...

  5. nuxt header 设置

    1. nuxt.config.js 中配置全局 2. 在单页面设置单独

  6. 众所周知,static修饰的成员只实例化一次,而string类型每次赋值都会重新创建一个实例,那么用static修饰string呢?

    string 类型每次实例化都会重新创建一个实例: 解释:string 类型重载了运算符 “=” ,每次 “=” 操作都是一次 “new”. static 修饰符的成员只实例化一次?? 解释:这个说法 ...

  7. Raft——可理解的分布式一致性算法

    Raft  Understandable Distributed Consensus http://thesecretlivesofdata.com/raft/ 一个直观的动画,便于理解raft算法. ...

  8. 笔记:Python列表和元组

    列表 列表和字符串之间的转换 >>> li = list('hello') >>> li ['h', 'e', 'l', 'l', 'o'] >>> ...

  9. element-UI 点击一行,背景色变化

    代码: @row-click="rowClick" 当某一行被点击时会触发该事件 :row-class-name="tableRowClassName"  可以 ...

  10. docker gitlab backup

    说明:下面命令中带有<your container name>字,是 gitlab 容器的名称,请按实际情况进行代替 在创建备份之前,你不需要停止任何东西 docker exec -t & ...