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所在的整个方块堆之上}.输出相应的答案. 解法: ...
随机推荐
- 制作linux根文件系统
转载地址:http://wenku.baidu.com/view/2cb1b4707fd5360cba1adb14.html 2011十一月 24 转 嵌入式根文件系统制作 (3985) (0) 转自 ...
- shell 检查文件夹是否包含文件,或者只是空文件
empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...
- mysql中explain详解
explain语法 有两种用法: 1.EXPLAIN tbl_name 2.EXPLAIN [EXTENDED] SELECT select_options 为了更好的说明它,我们需要建两张表, ...
- 「CSP-S 2019」树的重心
题目 考场上送\(75pts\)真实良心,正解不难:考虑直接对于每一个点算割掉多少条边能使得这个点成为重心,不难发现对于一个不是重心的点,我们要割掉的那条边一定在那个大于\(\lfloor \frac ...
- [POI2011]IMP-Party
题目 不难发现\(\frac{2}{3}n-\frac{1}{3}n=\frac{1}{3}n\)(雾 一个团要求点之间两两有边,于是我们枚举两个点,如果这两个点之间没有边相连,那么就删掉这两个点,由 ...
- sanic+aiohttp爬虫demo(爬图片,新闻,数据)
直接上代码,都是很简单的一些demo,爬取的网站,都没有什么加密措施,所以应该不涉及违法数据,哈哈 1.爬取网页数据(aiohttp+sanic+scrapy+xpath解析html) from sa ...
- 案例2:tab栏切换
<style> body,ul,li,div{margin:0;padding: 0;} ul{font-size: 0px;} .tab_list{ border: 1px gray s ...
- Pregel Master
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoa
最近运行ssm项目遇到tomcat启动报错: 解决办法,右击项目选择properties 在Deployment Assembly add 选择maven dedependencies 项目成功运行 ...
- 27 string类中常用的方法列表
1. 获取方法 int length() 获取字符串的长度 char charAt(int index) 获取特定位置的字符 (角标越界) int indexOf(String str) 获取 ...