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. Codeforces VK Cup Finals #424 Div.1 C. Bamboo Partition(数论)

    题目要求符合以下条件的最大的d 化简得 注意到 最多只有2*sqrt(a[i]-1)种取值,也就是一共最多有n*sqrt(10^19)种取值,于是枚举一下d,计算出符合上上式的最大的d更新答案,然后d ...

  2. apache出现You don't have permission to access / on this server. 提示

    今天在新的linux上跑原来的代码,使用的虚拟主机的模式进行操作.几个相关的网站放在一个文件里,想法是通过网站列出的目录进行相应的网站进行操作.一切设置完成后,在浏览器中运行出现在You don't ...

  3. 中国MOOC_面向对象程序设计——Java语言_第3周 对象容器_1查找里程

    第3周编程题 查看帮助 返回   第3周编程题.注意程序(包括注释)中不能出现汉字. 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后 ...

  4. Kubernetes - Start containers using Kubectl

    In this scenario, you'll learn how to use Kubectl to create and launch Deployments, Replication Cont ...

  5. LightOJ 1135 - Count the Multiples of 3 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1135 题意:给定两个操作,一个对区间所有元素加1,一个询问区间能被3整除的数有多少个. ...

  6. [Luogu 3224] HNOI2012 永无乡

    [Luogu 3224] HNOI2012 永无乡 特别水一个平衡树题. 不认真的代价是调试时间指数增长. 我写的 SBT,因为 Treap 的 rand() 实在写够了. 用并查集维护这些点的关系, ...

  7. Ubuntu12.04 安装nginx和mongo过程

    1.安装php和php-cgi apt-get install php5 php5-cgi 2.安装 nginx apt-get install nginx 3.安装 MongoDB apt-get ...

  8. intellj idea点击导航栏打开的一个类,怎么才能定位到类的目录

  9. 【Foreign】Game [博弈论][DP]

    Game Time Limit: 20 Sec  Memory Limit: 512 MB Description 从前有个游戏.游戏分为 k 轮. 给定一个由小写英文字母组成的字符串的集合 S, 在 ...

  10. MongoDB 数据库(2)

    db.collectionName 集合对象 获取集合对象 db.getCollection('collection_name') e.g. db.getCollection("class0 ...