POJ1988 Cube Stacking (!hard)

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
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.

题目大意:(同银河舰队传说) 对于n块积木,进行两种操作:M a b 把a移到b上,C a 询问a下面有几块积木。

思路:用并查集和tot,sum数组维护,像银河一样简单AC。。。

code:

#include<iostream>

#include<cstdio>

using namespace std;

int fa[30001]={0},sum[30001]={0},len[30001]={0};

int rool(int x)

{

int j;

if (fa[x]!=x)

{

j=rool(fa[x]);

len[x]=len[x]+len[fa[x]];

fa[x]=j;

}

return fa[x];

}

int main()

{

int p,n,i,j,r1,r2,x,y;

char ch;

scanf("%d",&p);

n=30000;

for (i=1;i<=n;++i)

{

fa[i]=i;

sum[i]=1;

}

for (i=1;i<=p;++i)

{

scanf("%*c%c",&ch);

if (ch==‘M‘)

{

scanf("%d%d",&x,&y);

r1=rool(x);

r2=rool(y);

if (r1!=r2)

{

len[r1]=len[r1]+sum[r2];

sum[r2]=sum[r2]+sum[r1];

fa[r1]=r2;

}

}

else

{

scanf("%d",&x);

r1=rool(x);

printf("%d\n",len[x]);

}

}

}

时间: 2024-08-12 15:45:45

POJ1988 Cube Stacking (!hard)的相关文章

POJ 1988 Cube Stacking(转)

这道题的思路,就是每次记下该点到父结点的个数,并记录下其下的结点个数.之后,每次"C"时,将总的减去它所压的方块,即答案!!!(也是参考别人的~-?) <pre name="code" class="cpp">#include<stdio.h> #include<iostream> using namespace std; #define max 30010 struct node { int parent;/

poj1988 Cube Stacking(并查集

题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[1 6]上为[2 4 1 6]),C x为输出x下面有几个数. 思路:并查集每个集合以栈最下面的数为根,维护两个数组num[x]表示x所在集合节点总数,count[x]表示x下方节点个数.每次查找压缩路径的时候更新count(换父节点的时候每轮都把父节点的count加给儿子,就可以一直更新到x所在栈

poj1988 Cube Stacking

并查集的高效之处在于路径压缩和延迟更新. 在本题中需要额外维护子树的规模以及当前子树节点到跟的距离两个数组. 由于一个新的数必然是两棵树拼接而成,对于子树规模的更新直接相加即可, 对于节点到跟的距离: 我们让a树的根指向b树的根,同时更新a树根到(a+b)树根(即b树根)的距离为size_of(Tree(b))即可完成维护. http://poj.org/problem?id=1988 1 #include <cstdio> 2 #include <cstring> 3 #incl

POJ1988 Cube Stacking(并查集)

题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问含有x元素下面有多少个元素. 思路: 并查集,把每一堆看作一个栈,堆的下方看作栈顶.因为当我们知道栈中元素的总数,和某元素到“栈顶”的距离, 我们就能知道这个元素下面有多少元素.合并操作的时候,始终使用在下面栈的根来做合并之后的根,这样也就达到了栈中的根是栈中的“栈顶”元素的效果,我们只需在每个“栈

POJ1988 Cube stacking(非递归)

n有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: nM x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. nC x : 问方块x下面有多少个方块. n操作最多有 P (P<=100,000)次.对每次C操作,输出结果. 这题挺厉害的,真的不容易想 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<strin

加权并查集——(POJ1988)Cube Stacking

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 25647   Accepted: 8975 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start w

HDU 1988 Cube Stacking (数据结构-并查集)

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18900   Accepted: 6568 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start w

POJ 1988 Cube Stacking(并查集+路径压缩)

题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 另外一种操作是询问含有x元素以下有多少个元素. 用sum数组储存每一个栈中的元素个数.每次合并的时候将sum加到 父亲节点.也就是每一个栈的最底部. 用under数组储存当前节点以下有多少元素.每次合并的时候,就能够将顶端元素的under赋值为父节点也就是栈最底部的sum. void U

HDU 1988 Cube Stacking (数据结构-并检查集合)

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18834   Accepted: 6535 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start w