hdu 2818 Building Block
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
2009 Multi-University Training Contest 1 - Host by TJU
题意:
1 /*有N个砖头, 编号1—N(实际上是0-N),然后有两种操作,第一种是M x y 把x所在的那一堆砖头全部移动放到y所在的那堆上面。 第二种操作是 C x ,即查询x下面有多少个砖头 ,并且输出。 2 */
--->带权值的并查集
代码:
1 #include<cstring>
2 #include<cstdio>
3 #include<cstdlib>
4 #define maxn 30030
5 using namespace std;
6 int father[maxn];
7 __int64 rank[maxn],under[maxn];
8 int p;
9
10 void init(){
11
12 for(int i=0;i<maxn ;i++) {
13 father[i]=i;
14 rank[i]=1;
15 under[i]=0;
16 }
17
18 }
19
20 int fin(int x){
21
22 if(x == father[x])
23 return father[x];
24 int tem = father[x] ;
25 father[x] = fin(father[x]);
26 under[x]+=under[tem];
27
28 return father[x];
29
30 }
31
32 void Union(int a,int b){
33
34 int x = fin(a);
35 int y = fin(b);
36 if( x==y ) return ;
37 father[x] = y ; //将a所在的堆放在b的堆上
38 under[x] = rank[y];
39 rank[y] += rank[x];
40 rank[x] = 0;
41
42 }
43
44 int main()
45 {
46 char str[2];
47 int a,b;
48
49 while(scanf("%d",&p)!=EOF){
50 init();
51 while(p--){
52 scanf("%s",str);
53 if(str[0]=='M'){
54 scanf("%d%d",&a,&b);
55 Union(a,b);
56 }
57 else{
58 scanf("%d",&a);
59 fin(a);
60 printf("%I64dn",under[a]);
61 }
62
63 }
64
65 }
66 return 0;
67 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- BFE.dev前端刷题#58. 获取DOM tree高度
- 如何在Tungsten Fabric上整合裸金属服务器(附配置验证过程)
- 逐行阅读Spring5.X源码(三) BeanDefinition的实现类详解,拔高
- 逐行阅读Spring5.X源码(番外篇)BeanDefinition到底有多重要
- 逐行阅读Spring5.X源码(番外篇)AnnotatedBeanDefinitionReader的作用
- 逐行阅读Spring5.X源码(四) BeanFactory——核心容器bean工厂
- 逐行阅读Spring5.X源码(五) 初探BeanFactoryPostProcessor后置处理器,难,特别难。
- 逐行阅读Spring5.X源码(六) ClassPathBeanDefinitionScanner扫描器
- 逐行阅读Spring5.X源码(番外篇)自定义扫描器, Mybatis是如何利用spring完成Mapper扫描的
- 逐行阅读Spring5.X源码(七)扫描和注册神器 ConfigurationClassPostProcessor ,学此类者,胜过学九阳神功!胆小勿入!
- 「Mysql索引原理(三)」Mysql中的Hash索引原理
- RNN、lstm、gru详解
- 「Mysql索引原理(四)」单列索引
- 「Mysql索引原理(五)」多列索引
- 「Mysql索引原理(六)」聚簇索引