poj 1988 Cube Stacking 并查集的应用
来源:互联网 发布:嘉兴学院网络管理中心 编辑:程序博客网 时间:2024/05/21 22:41
这道题目题意是有有两种操作1、是将x移动到y的stack顶部。2、计数在x的stack中,在x之下的的cube个数。
主要思想是用arr[i]表示父亲节点,d[i]表示i之下有几个节点,s[i]表示以i为根的树共有几个节点。
在执行M时是直接找到a,b的父节点,然后合并,执行C时是查找答案。
#include<stdio.h>#include<String.h>#define maxn 300005int p;int arr[maxn],re[maxn],rank[maxn];int find(int x){if(arr[x]==x)return x;int temp =arr[x];arr[x]=find(arr[x]);rank[x]+=rank[temp];return arr[x];}void merge(int x, int y){x=find(x);y=find(y);arr[x]=y;rank[x]+=re[y];re[y]+=re[x];re[x]=0;}int main(){int i,a,b;char ch[5];for(i=1;i<=maxn;i++){re[i]=1;rank[i]=0;arr[i]=i;}scanf("%d",&p);for(i=1;i<=p;i++){scanf("%s",&ch);//getchar();if(ch[0]=='M'){scanf("%d%d",&a,&b);merge(a,b);}if(ch[0]=='C'){scanf("%d",&a);find(a);printf("%d\n",rank[a]);}}return 0;}
0 0
- 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 (并查集)
- 一个不错的Git教程
- 配置Andoid开发环境与配置PhoneGap开发环境
- JAVA设计模式-从抽象与实现说桥接模式
- ANT打包报MalformedByteSequenceException: 3 字节的 UTF-8 序列的的原因
- Unix 网络编程 dup和dup2函数
- poj 1988 Cube Stacking 并查集的应用
- poj 1830 开关问题 高斯消元
- CubieTruck安装Cubian X1
- Android 指南针
- Oracle 归档与非归档的切换
- 第12周项目3返回斐波那契数列的第n个数
- C++运算符重载讲解与经典实例
- 分享一个自己做的一个3DM手机客户端
- 安捷影音播放器 v16.0.3 官方版