Problem Description
John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N。Initially, there are N piles, and each pile contains one block. Then John do some operations P times ( <= P <= ). 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
M
C
M
M
C
C
Sample Output

 
Source
 

题目大意:有N个piles(按序编号),每次有两种操作:

 

M x y表示把x所在的那一堆全部移到y所在的那一堆

 

C x 询问在x之下有多少个方块

 

解决方法:使用并查集(路径压缩)实现,然后用count[X]表示X所在的那一堆总共多少个piles,under[x]表示x之下有多少个piles。

 

首先,每次操作我们合并两个集合(如果原来在同一集合中除外),count[X]是每次操作可以直接实现的,就是把两堆的数目相加,很容易(初始值为1)。那么当某次移动操作发生时,首先确定x所在的那一堆最底部的X以及y所在那一堆最底部的Y,那么under[X]的数目就是另外一堆piles的总数count[Y],有了这个条件,在接下去的操作中,就可以根据FIND(x)递归去一边寻找根一边更新其他未知的under[x]。

 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 30006
int fa[N];
int under[N];//下边的个数
int cnt[N];//所在堆的堆个数
void init(){
for(int i=;i<N;i++){
fa[i]=i;
under[i]=;
cnt[i]=;
}
}
int find(int son){
if(fa[son]!=son){
int t=find(fa[son]);
under[son]+=under[fa[son]];
fa[son]=t;
}
return fa[son];
//return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2)return;
under[root1]=cnt[root2];
cnt[root2]+=cnt[root1];
fa[root1]=root2;
}
int main()
{
int n;
while(scanf("%d",&n)==){
init();
char s[];
int x,y;
for(int i=;i<n;i++){
scanf("%s",s);
if(s[]=='M'){
scanf("%d%d",&x,&y);
merge(x,y);
}
else{
scanf("%d",&x);
find(x);
printf("%d\n",under[x]);
}
}
}
return ;
}
Recommend

hdu 2818 Building Block (带权并查集,很优美的题目)的更多相关文章

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

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

  2. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  3. Hdu 2047 Zjnu Stadium(带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  5. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  6. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. How Many Answers Are Wrong HDU - 3038 (经典带权并查集)

    题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...

  8. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  9. HDU 3047 Zjnu Stadium(带权并查集)

    题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...

随机推荐

  1. 根据请求头跳转判断Android&iOS

    if(navigator.userAgent.match(/Android/i)) { window.location = 'http://apk.hiapk.com/m/downloads?id=c ...

  2. 遍历Jenkins全部项目的配置

    随着任务的增多.须要一个脚本能够检查全部的jenkins project的配置.比方提取任务计划配置,开发人员信息等. 首先要能够得到全部的project名称. 能够通过REST API实现: htt ...

  3. android启动优化

    ############################################## # power on till android lock screen comes up # # get ...

  4. HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)

    Isabella's Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. [小技巧] Python 脚本暴力破解 HC2600 机顶盒管理密码

    家里最近接入了广电有线电视,配了三个创维 HC2600 机顶盒,并且每个机顶盒还带有无线路由器功能. 免费赠送 Internet 接入服务倒也没什么,不过机顶盒内置的 WIFI 实在有点寒酸:只支持 ...

  6. Python 正则表达试

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  7. hdu 2033

    水题 AC代码: #include <iostream> using namespace std; int main() { int i,j,n,k,a[100],b[100]; cin& ...

  8. 深度克隆(对象、数组)--------百度IFE前端task2

    var srcObj = { a: 1, b: { b1: ["hello", "hi"], b2: "JavaScript" }}; co ...

  9. Win7下部署 .NET MVC网站 之 HTTP错误 403.14-Forbidden 解决方法

    今天在 IIS 7 发布MVC 站点时 遇到 ”HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容 “ 的错误提示. 一番折腾后发现在web.config 中加入 ...

  10. 引入css外部样式表

    前言 为什么会有这篇文章,外部引入样式有什么好谈的,不外乎就是 <link rel="stylesheet" href="style.css" /> ...