hdu 2818 Building Block (带权并查集,很优美的题目)
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.
The first line contains integer P. Then P lines follow, each of which contain an operation describe above.
Output the count for each C operations in one line.
M
C
M
M
C
C
题目大意:有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 ;
}
hdu 2818 Building Block (带权并查集,很优美的题目)的更多相关文章
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- hdu 3047–Zjnu Stadium(带权并查集)
题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
- HDU 3047 Zjnu Stadium(带权并查集)
题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...
随机推荐
- Object-C 类定义 -- 笔记
OC类分为两个文件,一个是.h文件,一个是.m文件 .h文件 存放类,函数的申明 .文件 存放类的具体实现 类申明使用关键字 @interface @end来申明 类实现使用关键字@implement ...
- 【C#多线程】C#多线程 Thread 开发基础
引用 using System; using System.Threading; 多线程代码 Thread mainthread = new Thread(ExecuteThread); mainth ...
- [React Testing] Children with Shallow Rendering
When testing React components, we often want to make sure the rendered output of the component match ...
- 每天进步一点点——Linux系统时间来处理
转载请注明出处:http://blog.csdn.net/cywosp/article/details/25839551 在程序中时间处理往往是一个麻烦的事.Linux系统提供了非常多关于时间处理的函 ...
- C#自定义List类
代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- Linux 数据 CD 刻录
http://www.cyberciti.biz/tips/linux-burning-multi-session-cds-on-linux.html #mkisofs -dvd-video -inp ...
- 使用VS Code开发Angular 2应用程序所需配置文件的解析
目录 package.json typings.json tsconfig.json launch.json settings.json tasks.json package.json: 这是项目的基 ...
- App轮播图
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- code first 尝试
建表: 1.先用EF连接数据库,配置connectionStrings <configSections> <!-- For more information on Entity Fr ...
- FeatureClass Copy
http://edndoc.esri.com/arcobjects/9.2/NET/c45379b5-fbf2-405c-9a36-ea6690f295b2.htm Method What is tr ...