poj1988Cube Stacking(并查集+路径压缩)
2016-05-09 20:27
218 查看
本题依然是并查集的应用,与上一个监狱的问题不同,本题计算的是距离,通过记录通往自己父节点距离的相对距离,与父节点到根节点(代表元素)的距离依次相加
路径压缩时每次都要更新节点。
路径压缩时每次都要更新节点。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn=30001; int p; struct { int father,tot,dis; }block[maxn]; void in() { for(int i=0;i<maxn;++i) { block[i].dis=0; block[i].father=i; block[i].tot=1; } } int getanc(int a) { if(block[a].father==a) return a; int ret; ret=getanc(block[a].father); block[a].dis+=block[block[a].father].dis; block[a].father=ret; return ret; } void union_set(int a,int b) { int aa=getanc(a); int bb=getanc(b); block[bb].dis=block[aa].tot;//b元素的代表元素到代表元素的距离 block[bb].father=aa;//b元素的代表元素父节点更新 block[aa].tot+=block[bb].tot;//元素总个数更新 } void print(int a) { int aa=getanc(a); printf("%d\n",block[aa].tot-block[a].dis-1); //代表元素总个数-当前元素到代表元素的距离-本身=其下元素个数 } int main() { scanf("%d",&p); char s; int x,y; in(); while(p--) { getchar(); scanf("%c",&s); if(s=='M') { scanf("%d %d",&x,&y); union_set(x,y); } else {scanf("%d",&x);print(x);} } return 0; }
相关文章推荐
- html5使用图像-绘制图像
- 欢迎使用CSDN-markdown编辑器
- 我理解的RxJava
- iOS中判断基础字符(大小写、数字等的判断)
- tomcat7的安装与配置、及Servlet部署
- 1004 放盘子(数学公式)
- cPanel如何修改为中文显示
- e.preventDefault
- HTML布局
- Error:Execution failed for task ':app:preDexDebug'. > com.android.ide.common.process.ProcessExceptio
- hdu1104Remainder(BFS)
- 网页背景图固定不动, 不跟随滚动条滚动
- uva - Score
- 【Redis】:Jedis 使用
- 重新学javaweb---文件上传1
- eclipse远程debug
- 优化方法
- 二十一、享元设计模式
- 设计模式之:模版设计模式
- 第7周 C语言程序设计(新2版) 例题1.5.4 统计行数、单词数及字符数