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 数组属性和方法
- RTSP协议视频平台EasyNVR证书配置界面上传文件地址自动填写错误怎么处理?
- 开发RTSP/RTMP/GB28181/海康SDK/EHome视频融合平台EasyCVR,使用vue-cli3项目搭建多页面模式的方法
- 互联网视频直播&点播平台RTMP推流组件EasyRTMP在弱网环境下推流稳定吗?会不会有推流失败的问题?
- 安防融合视频云服务EasyCVR集成海康EHome协议实现设备录像回看返回会话ID为-1是什么情况?
- git .gitignore 忽略规则的匹配语法
- vue 初始化高德地图
- js -- 判断数组是否为空?
- 手机没网了,却还能支付,这是什么原理?
- grub异常
- 34.Python字符串替换方法translate
- 35.Python字符串格式运算符%
- 好一个 Spring Boot 开源在线考试系统!解决了我的燃眉之急
- OC基础--数据类型与表达式
- OC基础--字符串
- 编写优雅的 commit message 并自动生成 changelog