题目大意:

以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. Dubbo入门到精通学习笔记(十五):Redis集群的安装(Redis3+CentOS)、Redis集群的高可用测试(含Jedis客户端的使用)、Redis集群的扩展测试

    文章目录 Redis集群的安装(Redis3+CentOS) 参考文档 Redis 集群介绍.特性.规范等(可看提供的参考文档+视频解说) Redis 集群的安装(Redis3.0.3 + CentO ...

  2. 12、testng.xml指定运行测试包、测试类、测试方法

    目录如下: TestFixture.java 代码如下: package com.testng.cn; import org.testng.annotations.*; public class Te ...

  3. GF学习未解之谜

    1.很奇怪事件管理器里面的用到的订阅事件里面的ID是通过typeof(xxx).GetHashCode()得到的,怎么解决id重复的问题? 2.log系统里面是不是直接全部当做多参数解决问题比较好?

  4. node.js 中的 fs (文件)模块

    记录 fs 模块的方法及使用 1. fs.stat 获取文件大小,创建时间等信息 // 引入 fs 模块 const fs = require('fs'); fs.stat('01.fs.js', ( ...

  5. 小程序 webview踩坑

    很多功能是需要调用wx.confing的 wx.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开, ...

  6. linux模范配置文件格式

    模范配置文件 #--------------------------------------------------------------------- # Global settings #--- ...

  7. 7年Java后端被淘汰,一路北漂辛酸史。。。

    作者:春天花会开foryou oschina.net/question/3465562_2281392 今天分享一位同行的经历: 本人Java开发6年半不到7年的样子. 英语专业,临毕业跟着隔壁专业去 ...

  8. 高级UI晋升之常用View(三)上篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下两个内容来介绍常用View: [RecycleView] [Ca ...

  9. CentOS 7 用 yum 安装 Nginx

    在 CentOS 7 中,直接使用 yum 安装 Nignx 会提示无下载源.因此,需要添加 Nginx 的下载源到 yum: sudo rpm -Uvh http://nginx.org/packa ...

  10. 看了Google编码规范,我突然有个感觉

    那么个编码规范,充分体现了西方人的自我感觉良好,以及以自己为中心的程度, 以及西方人对待事物的双重标准.