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. A glimpse of Support Vector Machine

    支持向量机(support vector machine, 以下简称svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本篇希望在正篇提供一个svm的简明阐述,附录则提 ...

  2. mysql、mariadb安装和多实例配置

    本文目录:1. mysql单实例安装 1.1 rpm安装mysql 1.2 通用二进制包安装mysql 1.2.1 初始化数据库 1.2.2 安装后的规范化操作 1.3 编译安装 1.3.1 编译安装 ...

  3. Quartz源码——JobStore保存JonDetail和Trigger源码分析(一)

    我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! {0} :表的前缀 ,如表qrtz_trigger ,{0}== qrtz_ {1}:quartz ...

  4. EOutOfResources EConvertError is not a valid integer value Unable to insert a line

    is not a valid integer value???project Teclaser_Single.exe raised exception class EOutOfResources wi ...

  5. 【JVM命令系列】javap

    命令基本概述 javap是JDK自带的反汇编器,可以查看java编译器为我们生成的字节码.通过它,可以对照源代码和字节码,从而了解很多编译器内部的工作.可以在命令行窗口先用javap -help看下j ...

  6. Linux入门之常用命令(10)软连接 硬链接

    在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件时,索引结点被复制到内存在,从而实现文件的快速访问. ...

  7. spring jar包

    org.springframework.aop- 3.0.0.RELEASE--------------------Spring的面向切面编程,提供AOP(面向切面编程)实现 org.springfr ...

  8. 洗礼灵魂,修炼python(6)--活起来的代码+列表

    活起来的用法: 使用input内置函数 注意python2中和python3中,input函数是不太一样的,python2中,input用户传入什么类型就是什么类型而python3中,不管传入什么类型 ...

  9. 机器学习-KNN分类器

    1.  K-近邻(k-Nearest Neighbors,KNN)的原理 通过测量不同特征值之间的距离来衡量相似度的方法进行分类. 2.  KNN算法过程 训练样本集:样本集中每个特征值都已经做好类别 ...

  10. Long Long Message (poj2774 后缀数组求最长公共子串)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 19206   Accepted: 79 ...