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. Transparency Tutorial with C# - Part 1

    Download demo project - 4 Kb Download source - 6 Kb Download demo project - 5 Kb Download source - 6 ...

  2. XML解析器(TinyXML)的使用指南

    关于XML文件的解析方法的引导, 大家可以去试试这个工具(TinyXML) 1.首先下载TinyXML库的文件,这里给出链接,大家自己去下吧,记着要上国际http://prdownloads.sour ...

  3. 减少iOS应用程序安装包大小

    安装包优化大小方法: <资源优化> 1.去除无用资源 通过几次项目的升级后,项目中会出现一些没有用到的图片.这些图片在我们导入到项目中后,之后项目升级过程后并没有再次用到. 那这些图片我们 ...

  4. Vi操作技巧

    Vi操作技巧: :nu    显示当前所在行的行号 :set nu    显示全部行号 :set nonu        取消显示行号 /字符串    查询字符串,按n查询下一个,按N查询上一个 持续 ...

  5. TreeGrid( 树形表格)

    本节课重点了解 EasyUI 中 TreeGrid(树形表格)组件的使用方法,这个组件依赖于DataGrid(数据表格)组件 一. 加载方式//建立一个 JSON 文件[{"id" ...

  6. 【转】使用 Eclipse 调试 Java 程序的 10 个技巧

    你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...

  7. for语句应用:乘法表

    乘法表: for语句应用: 1: 2: public class chengfa { 3: public static void main(String[] args) { 4: //int i; 5 ...

  8. [转]Xcode的重构功能

    Xcode提供了以下几个重构功能: Rename Extract Create Superclass Move Up Move Down Encapsulate 在菜单栏中的位置如下图: 在代码区里直 ...

  9. 服务器端操作Cookie[2]

    服务器端操作Cookie,主要注意会使用以下三个类: HttpCookie,HttpResponse,HttpRequest 关于HttpCookie: 属性 描述 例子 Domain 获取或设置与此 ...

  10. 数据库元数据分析Demo

    核心类:DatabaseMetaData.ResultSetMetaData 1 System.err.println("********************************** ...