题面:

    约翰和贝茜在玩一个方块游戏.编号为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. 使用Hive的正则解析器RegexSerDe分析nginx日志

    1.环境: hadoop-2.6.0 + apache-hive-1.2.0-bin 2.使用Hive分析nginx日志,站点的訪问日志部分内容为: cat /home/hadoop/hivetest ...

  2. 分享微软官方Demo用的SharePoint 2010, Exchange 2010, Lync 2010虚拟机

    微软官方有一套专门用于SharePoint 2010, Exchange 2010 Demo的虚拟机:SharePoint 2010: Information Worker Demonstration ...

  3. linux下apache+openssl配置记录

    软件环境 Apache Httpd 2.2.29 (http://httpd.apache.org ) OpenSSL 1.0.1h (http://www.openssl.org/source ) ...

  4. mongoDB学习笔记——安装及启动

    WINDOWS环境下: 一.安装 步骤一:  下载MongoDB  url下载地址:  http://downloads.mongodb.org/win32/ 步骤二:  设置MongoDB程序存放目 ...

  5. arm linux串口蓝牙工具移植及使用【转】

    本文转载自:http://blog.csdn.net/hclydao/article/details/51451725 p6212中串口蓝牙在linux下的使用记录 一.linux蓝牙工具移植 主要使 ...

  6. Google android source code build 问题总结【转】

    本文转载自:http://light3moon.com/2015/01/31/Google%20android%20source%20code%20build%20%E9%97%AE%E9%A2%98 ...

  7. iOS社会化分享(干货)

    一.苹果原生集成的社会化分享 1.哪些平台 (1)Twitter (2)FaceBook (3)Flickr (4)Vimeo (5)新浪微博  :iOS6 (6)腾讯微博 : iOS7 2.框架 : ...

  8. scws

    SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统). 这是一套基于词频词典的机械式中文分词引擎,它能将一整段的中文文本基本正确地切分成 ...

  9. BZOJ 4517 组合数+错排

    思路: 预处理错排 然后C(n,m)*s[n-m-1]就是答案了 特判n-m-1<0 //By SiriusRen #include <cstdio> using namespace ...

  10. POJ 2823 线段树 Or 单调队列

    时限12s! 所以我用了线段树的黑暗做法,其实正解是用单调队列来做的. //By SiriusRen #include <cstdio> #include <cstring> ...