HDU - 2818
Building Block
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3563 Accepted Submission(s): 1072
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.
0
2
/***
题意:给出一些数,并给对这些数进行操作;
'M a b'代表把a堆加到b堆上面,
‘C a ' 代表查询当前点a下面有多少个
做法:并查集。
***/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
#define maxn 300000 + 3000
int fa[maxn]; ///记录父节点
int sum[maxn]; ///记录当前堆的总和
int under[maxn]; ///带表当前节点下有多少pile
int n;
void Init()
{
for(int i=; i<=n; i++)
{
sum[i] = ;
fa[i] = i;
}
memset(under,,sizeof(under));
}
int Find(int u)
{
int tmp;
if(u != fa[u])
{
tmp = Find(fa[u]);
under[u] += under[fa[u]];
fa[u] = tmp;
}
return fa[u];
}
void Union(int x,int y)
{
int X,Y;
X = Find(x);
Y = Find(y);
if (X!=Y)
{
under[X] = sum[Y]; //X是当前堆(集合)中最底部的,直接更新under[]
sum[Y] += sum[X]; //直接更新Y这堆(集合)的高度(总共多少个Piles)
fa[X] = Y; //合并
}
}
int main()
{
while(~scanf("%d",&n))
{
Init();
char ch[];
int u,v,w;
while(n--)
{
scanf("%s",ch);
if(ch[] == 'M')
{
scanf("%d %d",&u,&v);
Union(u,v);
}
else
{
scanf("%d",&w);
Find(w);
printf("%d\n",under[w]);
}
}
}
return ;
}
HDU - 2818的更多相关文章
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- 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(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- 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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...
随机推荐
- BZOJ3140:[HNOI2013]消毒——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3140 https://www.luogu.org/problemnew/show/P3231 最近在 ...
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- bzoj3232
Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到 ...
- 直通BAT面试算法精讲课1
1.有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树. 给定二叉树的根结点root,请返回打印结果,结果按照每一层一个数组进行储存,所有数组的顺序按照层数从上往下,且每一层的数组内元素按照从左往右 ...
- [solr]solr的安装
solr是什么? 翻译: SolrTM is the popular, blazing fast open source enterprise search platform from the Apa ...
- opencv学习笔记二
1,读取照片(imread()) 2,处理照片(cvtcolor()) 3,命名窗口(namewindow()) 4,显示照片(imshow()) 5,保存照片(imwrite()) #include ...
- hdoj 1299 Diophantus of Alexandria
hdoj 1299 Diophantus of Alexandria 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1299 题意:求 1/x + 1/y ...
- 文件上传文件的权限--lnmp 环境配置,尤其整个项目复制过来动~~~
site_upload 需要是自己才建立动才会是root root 所有者:---不是root : root 上传不了文件,不是 777 就都可以上传的,也要看看是谁建立的文件夹: 打包 ...
- Maven命令行窗口指定settings.xml
maven命令行窗口指定特定settings.xml 在命令行界面指定settings.xml,命令如下: mvn install --settings c:\user\settings.xml 例如 ...
- 从零开始开发一款app,所想到的
我在知乎上看到这个问题http://www.zhihu.com/question/27645587.我在阅读了各位大牛的答案后,再加上自己的思考,就有了这篇文章的内容. 从零开始开发一款app ...