POJ 1988 Cube Stacking——并查集
来源:互联网 发布:平克弗洛伊德知乎 编辑:程序博客网 时间:2024/05/01 01:57
比较巧妙的并查集,考察对并查集的引申应用。维护三个数组:
a[i]表示i到pre[i]中间有方块的个数
pre[i]表示i所在集合编号,即根
c[i]表示以i为标志的集合中元素个数,只有当pre[i]=i的时候c[i]才有意义
合并时,只需进行如下操作:
1、对读入的x、y分别进行路径压缩,直到找到真根为止
2、合并x、y两个集合,主要步骤如下:
pre[x]:=y;a[x]:=c[y];c[y]:=c[y]+c[x];
这道题目倒是和银河英雄传说有些相似之处,可以对比着来做一下。
Program Cubes;//By_PoetshyConstmaxn=30000;Vari,n,p,q:Longint;pre,a,c:Array[1..maxn]of Longint;ch:Char;Procedure Combine(x,y:Longint);beginpre[x]:=y;a[x]:=c[y];inc(c[y],c[x]);end;Function Find_Set(i:Longint):Longint;var fa:Longint;beginif pre[i]=i then exit(i);fa:=pre[i];pre[i]:=Find_Set(pre[i]);inc(a[i],a[fa]);exit(pre[i]);end;Procedure Union(p,q:Longint);beginCombine(Find_Set(p),Find_Set(q));end;BEGINreadln(n);for i:=1 to maxn dobeginpre[i]:=i;c[i]:=1;a[i]:=0;end;for i:=1 to n dobeginread(ch);if ch='M' thenbeginreadln(p,q);Union(p,q);end elsebeginreadln(p);pre[p]:=Find_Set(p);writeln(a[p]);end;end;END.
- POJ 1988 Cube Stacking——并查集
- poj 1988 Cube Stacking 并查集
- poj-1988-Cube Stacking (并查集)
- poj 1988 Cube Stacking 并查集
- POJ 1988 Cube Stacking 并查集
- poj 1988 - Cube Stacking(并查集)
- poj 1988 Cube Stacking 并查集
- poj 1988 Cube Stacking 并查集
- POJ 1988 Cube Stacking(并查集)
- poj-1988-Cube Stacking (并查集)
- POJ 1988 Cube Stacking 并查集
- poj 1988 Cube Stacking(并查集)
- POJ 1988 Cube Stacking并查集
- 并查集-poj-1988-Cube Stacking
- poj-1988 Cube Stacking-并查集
- POJ 1988 Cube Stacking 并查集
- Cube Stacking POJ - 1988 并查集
- POJ-1988 Cube Stacking (并查集)
- CSDN 让我高看了
- jquery功能函数
- JS复制各种链接
- 直接通过ODBC读写Excel表格文件
- apache 修改 把根目录指向项目某个目录下
- POJ 1988 Cube Stacking——并查集
- Python的descriptor
- 特别关注最后一只
- 听高长寿敲代码
- jquery与ajax
- 程序江湖:第十五章 热衷传销的团队
- vmware serial port连接
- API Demos 2.3 学习笔记 (3)-- Android应用程序架构
- 如何将伪数组转换成数组