USACO2004 cube stacking /// 带权并查集 oj1302
题目大意:
以N ( 1 ≤ N ≤ 30,000 )个堆栈开始,每个堆栈包含一个单独的立方体。执行P(1≤ P ≤100,000)的操作。
有两种类型的操作:移动和计数。
*在移动操作中,将 包含方块X的堆栈 移动到 包含方块Y的堆栈 顶部。
*在计数操作中,在 包含立方体X的堆栈中 计算立方体X之上的立方体数量并报告该值。
编写一个可以验证游戏结果的程序。
* 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.
Print the output from each of the count operations in the same order as the input.
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
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的更多相关文章
- poj1988 Cube Stacking 带权并查集
题目链接:http://poj.org/problem?id=1988 题意:有n个方块,编号为1-n,现在存在两种操作: M i j 将编号为i的方块所在的那一堆方块移到编号为j的方块所在的那 ...
- POJ 1988 Cube Stacking(带权并查集)
哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...
- bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...
- 【BZOJ 3376】[Usaco2004 Open]Cube Stacking 方块游戏 带权并查集
这道题一开始以为是平衡树结果发现复杂度过不去,然后发现我们一直合并而且只是记录到最低的距离,那么就是带权并查集了,带权并查集的权一般是到根的距离,因为不算根要好打,不过还有一些其他的,具体的具体打. ...
- 初涉「带权并查集」&&bzoj3376: [Usaco2004 Open]Cube Stacking 方块游戏
算是挺基础的东西 Description 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- POJ 1988 Cube Stacking 【带权并查集】
<题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...
- 洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)
题目描述 约翰和贝茜在玩一个方块游戏.编号为 1\ldots n 1-n 的 n n ( 1 \leq n \leq 30000 1≤n≤30000 )个方块正放在地上,每个构成一个立方柱. 游戏开始 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
随机推荐
- 一个小BUG引发的思考。(论开发与测试之间的那点事)
标题不是“一个馒头引发的血案”. 言归正传:今天上午测试的时候,发现了一个BUG,如图: 一个用肉眼就能发现的BUG.原因当然是因为开发同事没有自测试,流入到了测试人员这里了. 无非是开发同事不严谨造 ...
- 记一次面经pm
记一次面经 介绍下你自己的这个项目. 浅谈一下数据结构. 参考网站 在我的记忆中,数据结构包括链表.线性表.栈与队列.数组.一些排序,比如冒泡排序.快速排序.希尔排序.堆排序等,还有一些树 ...
- TFS 中如何将项目加入已有的源代码管理器中?
Visual Studio 的某解决方案已经加入 Team Foundation Server,现在再将已经存在的项目加入到解决方案中,可是签入时,并没有把新加入的项目签入,怎么办呢? 在团队资源管理 ...
- kafka原理概念提炼
Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实 ...
- 最小生成树--Prim及Kruskal
//prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- C# WinfForm 控件之dev报表 XtraReport (五) 并排报表
有了前边的基础这个就很简单了,建一个容器报表 在detail,上放两个xrsubReport.再做两个明细报表,分别指定到xrsubreport就可以了
- oracle密码过期,改为原来的密码
我们都知道Oracle 数据库的用户的密码默认是有有效期限制的,特别是在Cloud上面的DB,有些用户是Cloud自动创建的,我们不知道原来的密码是什么,但是如果密码过期了,如果修改成新的密码,会影响 ...
- Eclipse+Marven + spring mvc 新建一个 Hello world 项目
1. 打开Eclipse,菜单 File->New->Marven Project. 2. 点击 Next, 3. 选择 marv ...
- leetcode-399-除法求值
方法一:dfs+图 class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], qu ...
- Mysql 编译报错 g++: internal compiler error: Killed (program cc1plus) 解决办法
g++: internal compiler error: Killed (program cc1plus) 解决办法 g++: internal compiler error: Killed (pr ...