Building Block

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 156 Accepted Submission(s): 70
 
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
2009 Multi-University Training Contest 1 - Host by TJU
 
Recommend
gaojie
 
/*
题意:两种操作:
M X Y : 将x所在的木块堆上的所有木块转移到y所在的木块堆
C X : 查询x所在的木块堆x下方的木块的数量 初步思路:很明显并查集 #错误:T了一发,没有压缩路径,wa了一发,压缩路径写惨了
*/
#include<bits/stdc++.h>
using namespace std;
int bin[];
int under[];//表示以i为根节点的数有多少木块(也就是i节点一下有多少木块)
int Rank[];//表示i节点所在的集合的木块数量
int findx(int x){
if(x!=bin[x]){
int tmp=findx(bin[x]);
under[x]+=under[bin[x]];
bin[x]=tmp;
}
return bin[x];
}
void merge(int x,int y){
int fx=findx(x);
int fy=findx(y);
if(fx!=fy){
bin[fx]=fy;//将x放到y节点上面
under[fx]+=Rank[fy];//x节点下面增加的肯定是y节点所在的全部木块
Rank[fy]+=Rank[fx];
}
}
int n;
char op[];
int x,y;
void init(){
for(int i=;i<;i++){
bin[i]=i;
Rank[i]=;
under[i]=;
}
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
while(n--){
scanf("%s",op);
if(op[]=='M'){
scanf("%d%d",&x,&y);
merge(x,y);
}else{//查找x这棵树有多少木块
scanf("%d",&x);
findx(x);
printf("%d\n",under[x]);
}
}
}
return ;
}

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(并查集,有点点复杂)

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

  3. Building Block[HDU2818]

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

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

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

  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. hdu2818 Building Block

    Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbere ...

  7. [HDOJ2818]Building Block(带权并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...

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

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

  9. hdu 2818 Building Block 种类并查集

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

随机推荐

  1. ThinkPHP控制器输出防止乱码小技巧

    在控制器中加一句:试试看 header('content-type:text/html;charset=utf-8');

  2. 『诡异的』VL10B创建外向交货单出错解决全过程

    一直觉得SAP STO的业务模式配置起来还是挺简单的,无非就是关联一下采购单与交货单的关系,以及相应工厂的装运数据,其他像主数据的设置也没有什么特别的.相比ICS模式,它少了IDOC的配置,所以还是很 ...

  3. 使用Xshell+Xmanager远程监控jvisualvm

    使用jvisualvm的remote方式监控服务器端jvisualvm时,不是很方便,因此通过local方式,应该是正路. 一.服务器端(Linux,最小安装模式,没有图形界面) 1.安装xauth ...

  4. bzoj1027 [HNOI2004]打鼹鼠

    [HNOI2004]打鼹鼠 2014年5月2日2,8605 Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠 ...

  5. 嵌入式linux开发之工具------tftp

    我在嵌入式linux开发中用到tftp的地方主要有2个方面: 1.是在嵌入式目标板启动时,bootloader启动时通过uEnv文件,下载dtb文件和kernel文件: 2.是在嵌入式目标板启动后,通 ...

  6. CSS图片垂直居中方法整理集合

    原帖链接:http://bbs.blueidea.com/thread-2666987-1-1.html 1.因为Opera,FF3,IE8均支持display:talbe;这些特性了,因此改进的办法 ...

  7. asp.net 的发布与执行

    asp.net的工程文件在测试完成后就是发布(部署)和执行. 发布 本地发布的例子 1,菜单项  生成--发布 2,如果是第一次发布,点击自定义,设置配置文件的名称 3,发布方法--文件系统,设置本地 ...

  8. Response.Write输出导致页面变形和页面白屏解决办法

    方法一:此方法应该是微软官方推荐的方法,但弹出时会造成页面白屏.Page.RegisterStartupScript("TestEvent", "<script&g ...

  9. webapp 启动 手机app

    <div class="downLoad clearfix"> <div onclick="jsOpenApp.Close(this);" c ...

  10. DevOps之归纳总结

    唠叨话 关于德语关我屁事的知识点,仅提供精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. DevOps归纳总结 <DevOps功能与性能>浏览器(饼干Cookie.会话Sessi ...