HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5935 Accepted Submission(s): 1838
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
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N();
int fa[N],sum[N],beh[N]; int find(int x)
{
if(fa[x]==x) return x;
int dad=fa[x];
fa[x]=find(fa[x]);
beh[x]+=beh[dad];
return fa[x];
}
void combine(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
beh[x]=sum[y];
sum[y]+=sum[x];
fa[x]=y;
}
void init()
{
for(int i=;i<N;i++) fa[i]=i,sum[i]=,beh[i]=;
} int main()
{
for(int p,u,v;~scanf("%d",&p);)
{
init();
for(char ch[];p--;)
{
scanf("%s%d",ch,&u);
if(ch[]=='M')
{
scanf("%d",&v);
combine(u,v);
}
else find(u),printf("%d\n",beh[u]);
}
}
return ;
}
HDU——T 2818 Building Block的更多相关文章
- 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(并查集,有点点复杂)
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 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 ...
- Building Block
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Building Block[HDU2818]
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 5538 House Building(模拟——思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...
- hdu 5538 House Building(长春现场赛——水题)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538 House Building Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- ActiveMQ学习笔记(4)----JMS的API结构和开发步骤
1. JMS的API结构 其实上图中的五个API在第一节中我们都已经使用到了.本节将会讲非持久化和持久化topic的使用. 2. JMS的基本开发步骤 1. 创建一个JMS工厂, Connectio ...
- 优动漫PAINT基础系列之拾色器教学
在优动漫PAINT中有类似Photoshop的拾色器功能么?在优动漫PAINT中,可以直接输入颜色数值选择颜色么?当然是可以的啦!怎么呼出拾色器界面~ 看这边... 前段时间小编有收到一些小伙伴的疑问 ...
- php xml 的基本操作类
class xmlMessage{ protected $doc; protected $rootKey; public function __construct() { $this->doc ...
- Ubuntu上面安装docker
1.先用uname -r查看系统的信息 2.安装docker的命令为 sudo apt install docker.io 3.然后就能实现安装了 但是:运行docker search golang, ...
- WPF 内存释放
原文:WPF 内存释放 在WPF 项目中引用到Transitionals 实现图片转场特效,发现每次图片转场内存都会增加,在每次转场后进行内存压缩 记录 解决方案如下: GC.Collect(); G ...
- unity Android在streamingAssets路径下文件无法读取的的解决方法
unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...
- 【codeforces 257D】Sum
[题目链接]:http://codeforces.com/problemset/problem/257/D [题意] 给你n个数字; 这n个数字组成的数组满足: a[i-1]<=a[i]< ...
- 洛谷 P3914 染色计数
P3914 染色计数 题目描述 有一颗NN个节点的树,节点用1,2,\cdots,N1,2,⋯,N编号.你要给它染色,使得相邻节点的颜色不同.有MM种颜色,用1,2,\cdots,M1,2,⋯,M编号 ...
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
继上一篇BeautifulSoup的高级应用,主要解说的是contents children descendants string strings stripped_strings.本篇主要解说.pa ...
- 利用艺术家的整数ID映射将标签转换为向量
<strong><span style="font-size:18px;">/*** * @author YangXin * @info Mapper选择艺 ...