Cube Stacking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 21567   Accepted: 7554
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 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.

Input

* Line 1: A single integer, P

* 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

Print the output from each of the count operations in the same order as the input file. 

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(并差集深度+结点个数)的更多相关文章

  1. POJ 1988 Cube Stacking( 带权并查集 )*

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

  2. POJ 1988 Cube Stacking(带权并查集)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 23678   Accepted: 8299 Ca ...

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

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18834   Accepted: 6535 Ca ...

  4. 加权并查集(银河英雄传说,Cube Stacking)

    洛谷P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.宇宙历七九九年,银河系的两大军事集团在 ...

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

    题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...

  6. [Usaco2004 Open]Cube Stacking 方块游戏

    题面:     约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱.    游戏开始后,约翰会给贝茜发出P(1≤P≤100000)个指令.指令有两种 ...

  7. poj.1988.Cube Stacking(并查集)

    Cube Stacking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submi ...

  8. Cube Stacking

    Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21350 Accepted: 7470 Case T ...

  9. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

随机推荐

  1. byte与sbyte的转换

    C#实现byte与sbyte的转换 byte[] mByte; sbyte[] mSByte = new sbyte[mByte.Length]; ; i < mByte.Length; i++ ...

  2. CSS自学笔记(5):CSS的样式

    CSS中拥有各种各样的样式表,而基本的样式有背景,文本,字体,链接,列表,表格,轮廓. 一.CSS-背景 CSS中允许用纯色背景,也允许用图片来创建复杂的个性背景. p {background-col ...

  3. 刷爆github小绿点

    转载请注明出处:https://ahangchen.gitbooks.io/windy-afternoon/content/kit/git/green_blush.html 工程地址,欢迎star!! ...

  4. Oracle EBS-SQL (SYS-4):sys_职责查询.sql

    select t.RESPONSIBILITY_NAME from apps.FND_RESPONSIBILITY_VL t where t.RESPONSIBILITY_NAME like '%MR ...

  5. 帝国cms留言表模板修改

    <form action="../../enews/index.php" method="post" name="form1" id= ...

  6. 2016 Multi-University Training Contest 4 总结

    第四场多校队伍的发挥还是相当不错的. 我倒着看题,发觉最后一题树状数组可过,于是跟队友说,便开始写,十分钟AC. 欣君翻译01题给磊哥,发现是KMP裸题,但是发现模板太旧,改改后过了. 11题是一道毒 ...

  7. dataGuard client 自动切换

    使用dataguard作为HA方案,要解决的一个问题在于:后台数据库发生了切换,client连接如何做到自动切到新的primary数据库上? 如果做通用的方案,需要客户端自己提供自动重连的能力,这点大 ...

  8. 解决ArcSDE图层名被占用的问题

    点击文章查看 当你发现上面那篇文章时你已经接近成功了,但还差最后一步! <!- -delete   from sde.GDB_OBJECTCLASSES where NAME = upper(' ...

  9. winfrom播放动态图片

    winfrom是不能直接加载的动态图片的.只能够自己写方法实现. 具体代码如下: using System; using System.Collections.Generic; using Syste ...

  10. android UI-EditText的长度监听慎用TextWatcher

    在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...