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. 动态加载JS

    <script> Date.prototype.format = function(format) { var o = { , //month "d+" : this. ...

  2. 用ubuntu下载电影:磁力链接,torrent,迅雷链接

    用ubuntu下载电影:磁力链接,torrent,迅雷链接 操作系统:Ubuntu 14.04 64位 需要软件:Ktorent, Amule 安装软件: sudo apt-get install k ...

  3. head标签掉到body里的问题

    HTML最开始的标签   <!DOCTYPE html>在其之前不能有任何的内容,否则head里的内容就会掉到body里,导致譬如网站的图标icon无法更新等的功能问题

  4. Servlet&jsp基础:第二部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. hdu 4946 Just a Joke(数学+物理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4969 Just a Joke Time Limit: 2000/1000 MS (Java/Others) ...

  6. Android 布局简要范例

    Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格. 而这些基础的布局框架结构很重要,需要玩的很熟悉.我将以前参考的部分代码示例,所做的相关实 ...

  7. 利用[后台]->[类别管理]为文章前后台添加类别名称【转】

    原网址:http://blog.csdn.net/yanhui_wei/article/details/7943176 1.给专题添加文章时,可以选择类别: 2.给文章模型.图片模型.下载模型的栏目下 ...

  8. HIHO线段树(成段)

    #include <stdio.h> #define lson l,mid,id<<1 #define rson mid+1,r,id<<1|1 ; ],lazy[ ...

  9. Linux之free命令

    from http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html free命令可以显示Linux系统中空闲的.已用的物理内存及swap ...

  10. spring.hibernate设置参数的位置问题

    1.spring的注解设置在set方法上 2.hibernate的注解设置在get方法上