题面:

    约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱.
   游戏开始后,约翰会给贝茜发出P(1≤P≤100000)个指令.指令有两种:
    1.移动(M):将包含X的立方柱移动到包含Y的立方柱上.
    2.统计(C):统计名含X的立方柱中,在X下方的方块数目.
    写个程序帮贝茜完成游戏.
 
解法:带权并查集,记录一个底面,顶面,x上面的立方体的个数。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
int n,dep[N],dis[N],fa[N];
int find(int x) {
if(x!=fa[x]) {
int fx=fa[x];
fa[x]=find(fx);
dep[x]=dep[fx];
dis[x]+=dis[fx];
}
return fa[x];
}
inline void merge(int x,int y) {
int fx=find(x),fy=find(y);
fa[fy]=fx;dis[fy]=dis[dep[x]]+;dep[fx]=dep[fy];
find(dep[fx]),find(dep[fy]);
}
int main() {
scanf("%d",&n);
for(int i=;i<N;i++) fa[i]=dep[i]=i;
char opt[];int x,y;
for(int i=;i<=n;i++) {
scanf("%s",opt);
switch (opt[]) {
case 'M' :scanf("%d%d",&x,&y);merge(x,y);break;
case 'C' :scanf("%d",&x);find(x);printf("%d\n",dis[dep[x]]-dis[x]);break;
}
}
}

[Usaco2004 Open]Cube Stacking 方块游戏

[Usaco2004 Open]Cube Stacking 方块游戏的更多相关文章

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

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

  2. BZOJ3376: [Usaco2004 Open]Cube Stacking 方块游戏

    [传送门:BZOJ3376] 简要题意: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000 ...

  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. bzoj 3376 [Usaco2004 Open]Cube Stacking 方块游戏——带偏移量的并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3376 带偏移量的并查集. #include<iostream> #include ...

  6. BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)

    题解 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #in ...

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

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

  8. poj.1988.Cube Stacking(并查集)

    Cube Stacking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submi ...

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

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 23678   Accepted: 8299 Ca ...

随机推荐

  1. 搭建strom 的开发环境 - local mode

    Setting Up a Development Environment This page outlines what you need to do to get a Storm developme ...

  2. Project Euler:Problem 33 Digit cancelling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  3. ASP环境下轻松实现报表的打印 (转)

    作者:zcg2000 一.前言 ASP在电子商务上应用广泛,报表的处理又有一些麻烦.本文介绍了在ASP中利用 本人写的Report Server Page脚本解释器实现报表的显示.打印. 二.准备工作 ...

  4. luogu2278 [HNOI2003]操作系统

    题目大意 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高.如果一个进程到达的时候 ...

  5. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

  6. P1390--公约数的和

    这个题比较狗,一开始没有啥思路,但是看完题解觉得还是比较好理解的.主要思路就是需要把每个数是几个数的最大公约数求出来,然后求和就行了.蓝书P124例九 设f(n) = gcd(1,n) + gcd(2 ...

  7. gcd&&exgcd&&斐蜀定理

    gcd就是求a和b最大公约数,一般方法就是递推.不多说,上代码. 一.迭代法 int gcd(int m, int n) { ) { int c = n % m; n = m; m = c; } re ...

  8. bzoj 4521 [ Cqoi 2016 ] 手机号码 —— 数位DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4521 数位DP,记录好多维状态: 写了半天,复杂得写不下去了,于是参考一下TJ... 练习简 ...

  9. POJ 1523 Tarjan求割点

    SPF Description Consider the two networks shown below. Assuming that data moves around these network ...

  10. C - Alice, Bob and Chocolate(贪心)

    Problem description Alice and Bob like games. And now they are ready to start a new game. They have ...