题目大意:

以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. Spring Boot整合Thymeleaf模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  2. Organizing Containers of Balls

    题目 David has several containers, each with a number of balls in it. He has just enough containers to ...

  3. Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP

    题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...

  4. c# 使用NOPI 操作Excel

    最近项目需要导出Excel,找来找去,微软有自己的Excel组件 using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;,但 ...

  5. UPDATE - 更新一个表中的行

    SYNOPSIS UPDATE [ ONLY ] table SET column = { expression | DEFAULT } [, ...] [ FROM fromlist ] [ WHE ...

  6. WordTEX

    https://www.andrew.cmu.edu/user/twildenh/wordtex/

  7. kubeadm生成的token重新获取

    当你的token忘了或者过期,解决办法如下: 1.先获取token #如果过期可先执行此命令kubeadm token create #重新生成token#列出tokenkubeadm token l ...

  8. UartAssist串口调试工具

    第一步安装UartAssist 第二步打开UartAssist 界面为 我们将我们的wifi模块连接电脑, 查看的端口号通过计算机管理设备管理器进行查看 根据要求发送数据 就可以了

  9. 使用kubeadm 搭建高可用集群 多master

    很快很简单 只要三分钟就能看完 三台服务器 k8s-vip  负载均衡器 k8s-master1 主节点一 k8s-master2 主节点一 官方文档 首先搭建负载均衡器 用的Haproxy yum ...

  10. linux服务器创建docker

    关于Docker在Linux服务器中的安装以及使用1 安装: yum install docker 2 启动: systemctl start docker.service 3.加入开机启动: sys ...