Building Block

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3563    Accepted Submission(s): 1072

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
 
Recommend
gaojie
/***
题意:给出一些数,并给对这些数进行操作;
'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的更多相关文章

  1. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  2. hdu 2818 Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  4. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  5. hdu 2818 Building Block(并查集,有点点复杂)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 2818(并查集,带权更新)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...

  8. HDU——T 2818 Building Block

    http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  9. POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

    题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...

随机推荐

  1. 牛客网 提高组第8周 T2 推箱子 解题报告

    推箱子 链接: https://ac.nowcoder.com/acm/contest/176/B 来源:牛客网 题目描述 在平面上有\(n\)个箱子,每个箱子都可以看成一个矩形,两条边都和坐标轴平行 ...

  2. JavaScript是没有域的限制

    baidu的通行证处理都是在二级域名passport.baidu.com中处理的,但是baidu很多地方登录都好像是用ajax处理的,他是怎么做的呢?研究了一下,发现一个小技巧. 在http://zh ...

  3. Eclipse中 properties 文件中 中文乱码

    在.properties文件写注释时,发现中文乱码了,由于之前在idea中有见设置.properties文件的编码类型,便找了找乱码原因 在中文操作系统中,Eclipse中的Java类型文件的编码的默 ...

  4. 题解 【luogu P1541 NOIp提高组2010 乌龟棋】

    题目链接 题解 题意: 有一些格子,每个格子有一定分数. 给你四种卡片,每次可以使用卡片来前进1或2或3或4个格子并拾取格子上的分数 每张卡片有数量限制.求最大分数. 分析 设\(dp[i]\)为第前 ...

  5. 51Nod 1002 数塔取数问题

    Input示例 4 5 8 4 3 6 9 7 2 9 5 Output示例 28 DP: 递推式: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+arr[i][j]; ...

  6. 归并排序Merge sort2

    原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...

  7. asyncio Lock,Queue

    # # total = 0 # # async def add(): # #1. dosomething1 # #2. io操作 # # 1. dosomething3 # global total ...

  8. Python随机选择Maya场景元素

    之前在公司参与的一个与国外合作的项目中,有一景需要动态.随机地选取场景中的一些物体,同时显示指定材质,当时是用Houdini的节点+Hscript 解决的: 今天用简洁优雅的Python在Maya中写 ...

  9. CAS 逻辑流程图

  10. Centos7下关于memcached的安装和简单使用

    在这里,由于用编译安装memcached服务端过于复杂,因此我选用依赖管理工具 yum 来实现 memcached 的服务端安装: [root@localhost /]# yum install -y ...