HDU 2818 Building Block(带权并查集)
来源:互联网 发布:哪个论坛源码好移动版 编辑:程序博客网 时间:2024/06/02 05:10
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818
题 意:有1----n个堆,每一堆有一个blocks,并且对他们进行两个操作:
1、把 X 所在的那一堆放到 Y 所在的那一堆上面。
2、询问 X 下面有多少个blocks。
思 路:使用并查集处理数据,并且将每个结点的高度与它之下的blocks数记录下来。
注:需要使用状态压缩,否则会超时。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <queue>#include <algorithm>#define m 30030typedef long long LL;int ben[m],high[m],under[m];int findx(int x)//状态压缩后的find函数{ if(x==ben[x]) return x; int z = findx(ben[x]); under[x] += under[ben[x]]; return ben[x]=z;}void mergex( int x, int y ){ int fx = findx(x); int fy = findx(y); if(fx!=fy) { under[fx]=high[fy];//记录该结点原有的blocks数 ben[fx]=fy; high[fy]+=high[fx];//初始化blocks数目 }}int main(){ int T; scanf ( "%d", &T ); for( int i = 0; i <= m; i ++ ) ben[i]=i,under[i]=0,high[i]=1; while ( T-- ) { char ch; scanf ( " %c", &ch ); if( ch == 'M' ) { int x,y; scanf ( "%d %d", &x, &y ); mergex(x,y); } else if( ch == 'C' ) { int x; scanf ( "%d", &x ); findx(x); printf("%d\n",under[x]); } } return 0;}
0 0
- HDU 2818 Building Block(带权并查集)
- HDU 2818 Building Block(带权并查集)
- HDU 2818 Building Block(带权并查集)
- HDU 2818 Building Block (带权并查集)
- HDU 2818 Building Block 带权并查集
- hdu 2818 Building Block(带权并查集)
- HDU 2818 Building Block 带权并查集
- hdu 2818 Building Block【基础带权并查集】
- hdu 2818 Building Block(带权并查集)
- hdu-2818-Building Block-带权并查集
- HDU 2818 Building Block (带权并查集)
- HDU 2818 Building Block 带权并查集
- HDOJ 2818 Building Block (带权并查集)
- HDU 2818 Building Block, poj 1988 Cube Stacking(带权并查集)
- POJ 1988 Cube Stacking || HDU 2818 Building Block 带权并查集
- HDU 2818 Building Block(并查集)
- hdu 2818 Building Block(并查集)
- HDU 2818 Building Block (并查集)
- ps aux|grep
- Android selector标签下的几种状态解读
- Meteor数据库mongodb查询
- Lesson01-07-20-C++高级数组
- 2012年5月SAT香港真题解析
- HDU 2818 Building Block(带权并查集)
- stl泛型算法
- Apache+PHP+MYSQL部署
- ECMAScript 5严格模式
- hdu 1010
- poj 2255
- 反射
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(二)
- C和C++中的const区别