poj 1988 Cube Stacking 并查集
来源:互联网 发布:dnf客户端优化 编辑:程序博客网 时间:2024/05/04 00:27
题目链接
题目意思就是将M a,b 就是将a所在的堆放到b所在的堆的上面。 C n 就是计算 n下面的箱子的个数
注意: 题目中需要开辟两个变量 记录堆中箱子的总个数和 本箱子上面的箱子个数
然后利用并查集,对两堆箱子进行合并,然后(上面的箱子的根节点)的 是(下面箱子根节点)的父节点。
然后更新(上面箱子的根节点的总个数)(记录的是两堆箱子合并后的总个数),(更新新面箱子的根节点的记录此箱子上面还有几个箱子的值)(这里注意因为查找根节点时会自动更新下面每个箱子的up(该箱子上面箱子的个数)的值 )。
post code:
#include<stdio.h>#include<string.h>struct node{ int father; int sum; int up;} cube[100000];char judge[3];int Find(int i){ int father=cube[i].father; //这里记住每个箱子的父节点 if(i==cube[i].father)return i; cube[i].father=Find(cube[i].father); cube[i].up+=cube[father].up; //然后对其up(该箱子上面的箱子个数)进行更新 return cube[i].father; }void Union(int top,int down){ int Ftop=Find(top); int Fdown=Find(down); if(Ftop!=Fdown){ cube[Fdown].father=Ftop; cube[Fdown].up+=cube[Ftop].sum; cube[Ftop].sum+=cube[Fdown].sum; } }int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ cube[i].father=i; cube[i].sum=1; cube[i].up=0; } int top,down; for(int i=1;i<=n;i++){ scanf("%s",judge); if(judge[0]=='M'){ scanf("%d%d",&top,&down); Union(top,down); } else { int box,Fbox; scanf("%d",&box); Fbox=Find(box); //查找过程中对于up值进行了更新 printf("%d\n",cube[Fbox].sum-1-cube[box].up); //根节点记录总个数-给箱子上面的箱子的个数-该箱子就是下面箱子的个数 } } }} /*100M 1 6M 2 4M 1 2C 4*/
- 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 (并查集)
- 【POJ 1988 Cube Stacking】+ 并查集
- 黑马程序员——String字符串,StringBuilder和StringBuffer,线性表: List,散列表
- Uva - 116 - Unidirectional TSP
- make menuconfig 添加/增加 配置选项
- Extjs4.0.7 中 TreeStore.load()出现 url undefine 错误的解决方法
- Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
- poj 1988 Cube Stacking 并查集
- rqnoj-153
- rqnoj-394
- uva10602 Editor Nottobad
- uva10714 Colliding Ants
- syntax error : missing ';' before '*'
- 如何破解myeclipse10.X以及导出WAR包问题
- 3ds max 导出插件——md5Exporter开场篇
- 利用python2.7打造1个web站点1