hdu2818 Building Block
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.
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
0
2
这题也是带权并查集,设三个数组pre[i],num[i](代表i所在集合的数字总数),root[i]表示i下面的数字总数。每次移动时输入两个数a,b,因为移动过程中a所在的所有数都移动到b所在集合所有数的上面,所以令a的祖先t1的父亲为b的祖先t2,这样下面递归的时候,root[i]可以递归相加。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
char s[10];
int pre[30005],num[30005],root[30005];
int find(int x)
{
int temp;
if(pre[x]==x)return x;
temp=pre[x];
pre[x]=find(pre[x]);
root[x]+=root[temp];
return find(pre[x]);
}
int main()
{
int p,i,m,j,a,b,t1,t2;
while(scanf("%d",&p)!=EOF)
{
for(i=0;i<=30005;i++){
pre[i]=i;num[i]=1;root[i]=0;
}
while(p--)
{
scanf("%s",s);
if(s[0]=='M'){
scanf("%d%d",&a,&b);
t1=find(a);t2=find(b);
if(t1==t2)continue;
pre[t1]=t2;
root[t1]+=num[t2];
num[t2]+=num[t1];
}
else if(s[0]=='C'){
scanf("%d",&a);
t1=find(a);
printf("%d\n",root[a]);
}
}
}
return 0;
}
hdu2818 Building Block的更多相关文章
- Building Block[HDU2818]
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2818 Building Block
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Building Block
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 2818 Building Block(并查集,有点点复杂)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...
随机推荐
- HDU6375双端队列
要点分析: 1.本题可以使用C++STL中的deque双端队列来方便解决(底层是一个双向的链表) 2.值得注意的是N的上限为150000,所以直接开这么大的空间会超内存,可以配合map一起使用 关于双 ...
- python模块详解 | filecmp
简介: filecmp是python内置的一个模块,用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单 两个主要的方法: filecmp.cmp(f1, f2[, shallow]) 比较 ...
- DHCP最佳实践(一)
这是Windows DHCP最佳实践和技巧的最终指南. 如果您有任何最佳做法或技巧,请在下面的评论中发布它们. 在本指南(一)中,我将分享以下DHCP最佳实践和技巧. 不要将DHCP放在您的域控制器上 ...
- 【栈和队列】2、栈的基本实现 - Java
简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 栈的实现 Stack<E> void push(E) E pop() E peek() int getSize() b ...
- LeetCode572. 另一个树的子树
题目 本题目一开始想要通过二叉树遍历KMP匹配,但看来实现比较复杂 不如直接暴力匹配,本题和LeetCode100.相同的树有共通之处 1 class Solution { 2 public: 3 b ...
- Sqli - Labs 靶场笔记(一)
Less - 1: 页面: URL: http://127.0.0.1/sqli-labs-master/Less-1/ 测试: 1.回显正常,说明不是数字型注入, http://127.0.0.1/ ...
- 【JAVA并发第三篇】线程间通信
线程间的通信 JVM在运行时会将自己管理的内存区域,划分为不同的数据区,称为运行时数据区.每个线程都有自己私有的内存空间,如下图示: Java线程按照自己虚拟机栈中的方法代码一步一步的执行下去,在这一 ...
- openpose c++ 配置教程 + python api
之前有介绍过基于tensorflow的openpose版本安装,但是我觉得没有caffe框架那么好用,很多功能也实现不了,比如调节net_resolution的调节,通过调节分辨率来提高检测的精确性和 ...
- [分享] 最流行的 10 个 JavaScript 库
1. Lodash https://github.com/lodash/lodash 一个工具库,用得还蛮多. 2. Chalk https://github.com/chalk/chalk 给终端加 ...
- 记录一下 ThreadLocal 与 WeakReference
ThreadLocal & WeakReference Thread整体的模块图 Thread -> ThreadLocalMap 对于继承了 WeakReference Entry本身 ...