Building Block

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3250    Accepted Submission(s): 973

Problem Description
John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N。Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds of operation:

M X Y : Put the whole pile containing block X up to the pile containing Y. If X and Y are in the same pile, just ignore this command. 
C X : Count the number of blocks under block X

You are request to find out the output for each C operation.

 
Input
The first line contains integer P. Then P lines follow, each of which contain an operation describe above.
 
Output
Output the count for each C operations in one line.
 
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
 
Sample Output
1
0
2
 
Source

题意:

/*有N个砖头, 编号1—N(实际上是0-N),然后有两种操作,第一种是M x y 把x所在的那一堆砖头全部移动放到y所在的那堆上面。 第二种操作是 C x ,即查询x下面有多少个砖头 ,并且输出。 */

--->带权值的并查集

代码:

 #include<cstring>
#include<cstdio>
#include<cstdlib>
#define maxn 30030
using namespace std;
int father[maxn];
__int64 rank[maxn],under[maxn];
int p; void init(){ for(int i=;i<maxn ;i++) {
father[i]=i;
rank[i]=;
under[i]=;
} } int fin(int x){ if(x == father[x])
return father[x];
int tem = father[x] ;
father[x] = fin(father[x]);
under[x]+=under[tem]; return father[x]; } void Union(int a,int b){ int x = fin(a);
int y = fin(b);
if( x==y ) return ;
father[x] = y ; //将a所在的堆放在b的堆上
under[x] = rank[y];
rank[y] += rank[x];
rank[x] = ; } int main()
{
char str[];
int a,b; while(scanf("%d",&p)!=EOF){
init();
while(p--){
scanf("%s",str);
if(str[]=='M'){
scanf("%d%d",&a,&b);
Union(a,b);
}
else{
scanf("%d",&a);
fin(a);
printf("%I64d\n",under[a]);
} } }
return ;
}

hdu 2818 Building Block的更多相关文章

  1. hdu 2818 Building Block(并查集,有点点复杂)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  3. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  4. hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...

  5. HDU——T 2818 Building Block

    http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. HDU—— 5159 Building Blocks

    Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...

  7. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

  8. Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. HDU - 2818

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. JSONObject.fromObject

    JSONObject.fromObjectjava.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRun ...

  2. Makefile文件简单整理

    .PHONY:clean main:hello.o gcc -o main hello.c hello.o:hello.c gcc -c hello.c clean: rm -f hello.o ma ...

  3. 自定义类型转换器converter

    作用:目前将日期转换成string,将string转换成我想要的类型   0509课件里讲 一.数据类型转换在web应用程序中,数据存在两个方向上的转换:1.当提交表单时  表单数据以字符串的形式提交 ...

  4. 最大后验估计 -- Maximum-a-Posteriori (MAP) Estimation

    最大后验估计是根据经验数据获得对难以观察的量的点估计.与最大似然估计类似,但是最大的不同时,最大后验估计的融入了要估计量的先验分布在其中.故最大后验估计可以看做规则化的最大似然估计.

  5. php 判断 xml 里是否存在某个节点

    参考网址:http://blog.csdn.net/crazyboy2005/article/details/6114454 DOMDocument中,怎样判断某节点是否存在呢? /* $xml-&g ...

  6. 认识DOS

    实验一.认识DOS实验 专业 物联网工程  姓名 叶慧敏 学号 201306104139 一. 实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法: (4)掌 ...

  7. E2 2014.5.8 更新日志

    增加功能 增加报价单功能,可以针对指定客户生成报价单,可以直接生成一个在线地址,直接把地址发给客户在线打开 传统的报价,先生成一个EXCEL,再传给客户,使用E2,这一切都变得简单,你可生成一个在线地 ...

  8. [转载] tmux 使用指南

    原文: https://danielmiessler.com/study/tmux/ tmux的用法和screen类似, 比screen好用一些, 不过需要单独安装

  9. hdu4717The Moving Points(三分)

    链接 需要特判一下n=1的时候 精度调太低会超时 #include <iostream> #include<cstdio> #include<cstring> #i ...

  10. Python学习(14)模块一

    目录 Python 模块 import语句 from ... import 语句 from ... import * 语句 定位模块 PYTHONPATH变量 命名空间和变量 dir()函数. glo ...