Cube Stacking(并差集深度+结点个数)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 21567 | Accepted: 7554 | |
Case Time Limit: 1000MS |
Description
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.
Input
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Output
1
0
2
题意:有n个从1到n编号的箱子,将每个箱子当做一个栈,对这些箱子进行p次操作,每次操作分别为以下两种之一:
1>输入 M x y:表示将编号为x的箱子所在的栈放在编号为y的箱子所在栈的栈顶.
2>输入 C x:计算编号为x的所表示的栈中在x号箱子下面的箱子数目.
题解:和龙珠那题一样,坑了半天,写的都醉了。。。
ac代码贴上:
#include<stdio.h>
#include<string.h>
const int MAXN=;
int pre[MAXN],s[MAXN],dep[MAXN];//s数组存当前树的节点数;
int find(int x){
if(x!=pre[x]){
int t=pre[x];
pre[x]=find(pre[x]);
dep[x]+=dep[t];
}
/* int i=x,j;
while(i!=r){
j=pre[i];pre[i]=r;i=j;
}*/
return pre[x];
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
// printf("%d %d\n",f1,f2);
if(f1!=f2){
pre[f2]=f1;
dep[f2]=s[f1];
s[f1]+=s[f2];
}
}
int main(){
int N,x,y;
char c[];//定义成s了,错的啊。。。。
while(~scanf("%d",&N)){
for(int i=;i<=N;i++)pre[i]=i,dep[i]=,s[i]=;
while(N--){
scanf("%s",c);
if(c[]=='M'){
scanf("%d%d",&x,&y);
merge(x,y);
}
else {
scanf("%d",&x);
int px=find(x);
// printf("s[%d]=%d %d\n",px,s[px],dep[x]);
printf("%d\n",s[px]-dep[x]-);
}
}
}
return ;
}
另一种解法超时:
#include<stdio.h>
#include<string.h>
const int MAXN=;
int pre[MAXN],s[MAXN];//s数组存当前树的节点数;
int find(int x){
while(x!=pre[x])
x=pre[x];
return x;
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2){
pre[f2]=f1;
s[f1]+=s[f2];
}
}
int getdep(int x){
int s=;
while(x!=pre[x]){
x=pre[x];
s++;
}
return s;
}
int main(){
int N,x,y;
char c[];//定义成s了,错的啊。。。。
while(~scanf("%d",&N)){
for(int i=;i<=N;i++)pre[i]=i,s[i]=;
while(N--){
scanf("%s",c);
if(c[]=='M'){
scanf("%d%d",&x,&y);
merge(x,y);
}
else {
scanf("%d",&x);
int px=find(x);
int d=getdep(x);
printf("%d\n",s[px]-d-);
}
}
}
return ;
}
Cube Stacking(并差集深度+结点个数)的更多相关文章
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- POJ 1988 Cube Stacking(带权并查集)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 23678 Accepted: 8299 Ca ...
- HDU 1988 Cube Stacking (数据结构-并检查集合)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18834 Accepted: 6535 Ca ...
- 加权并查集(银河英雄传说,Cube Stacking)
洛谷P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.宇宙历七九九年,银河系的两大军事集团在 ...
- POJ 1988 Cube Stacking(并查集+路径压缩)
题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...
- [Usaco2004 Open]Cube Stacking 方块游戏
题面: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000)个指令.指令有两种 ...
- poj.1988.Cube Stacking(并查集)
Cube Stacking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submi ...
- Cube Stacking
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21350 Accepted: 7470 Case T ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
随机推荐
- WinCE 5.0模拟器,在 win7 下安装后, VS2008里不显示
来源:http://blog.csdn.net/masterlonely/article/details/8302932 现状: Win7 32位旗舰版 VS2008 Team 在安装了: Windo ...
- Python核心编程笔记--随机数
#第一步引入模块 import random #第二步生成随机数 print random.randint(1,3);#[1,2,3]注意这里是闭区间.
- ThinkPHP中 按条件查询后列表显示
最近在项目中遇到了需要根据下拉框的条件筛选出符合条件的数据,然后进行列表显示的问题. 在ThinkPHP中进行列表显示的传统过程:通过在后台控制器中查询出数据,然后通过$this->assign ...
- Cloning Java objects using serialization
Sometimes you need to clone objects, and sometimes you can't use their clone method, and sometimes s ...
- Centos 升级MySQL版本或者Yum安装Mysql5.6
Centos 升级MySQL版本或者Yum安装Mysql5.6 1.从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/Cen ...
- Android软件版本更新
转的:适合新手学习,但在实际项目中不可这么做. 以下是我转的内容: ================================================================= ...
- dom4j和jaxp解析工具的
dom4j解析中的几个对象 node --branch --document --element --commment --attribute --text branch --document --e ...
- curl返回值写入内存的场景
直接上代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cur ...
- MATLAB中求矩阵非零元的坐标
MATLAB中求矩阵非零元的坐标: 方法1: index=find(a); [i,j]=ind2sub(size(a),index); disp([i,j]) 方法2: [i,j]=find(a> ...
- localhost简介、localhost与 127.0.0.1 及 本机IP 的区别
localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ...