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

Source

唉,一个并查集的题,改了好久。。。终于a了。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int up[];//该数上面的数
int n[];//该集合总数
int a[];//保存他的父节点 int father(int t){
if(t==a[t]){
return t;
}
int fa=a[t];
a[t]=father(a[t]);
up[t]+=up[fa]; return a[t];
} int main()
{
int p;
//char c;
int t1,t2;
int t3;
while(scanf("%d",&p)!=EOF){
for(int i=;i<=p;i++){
a[i]=i;
up[i]=;
n[i]=;
}
char s[];
for(int i=;i<p;i++){
scanf("%s",s);
if(s[]=='M'){
scanf("%d%d",&t1,&t2);
int fa=father(t1);
int fb=father(t2);
if(fa!=fb){
a[fb]=fa;
up[fb]+=n[fa];
n[fa]+=n[fb];
}
}else{
scanf("%d",&t3);
int ans=father(t3);
printf("%d\n",n[ans]-up[t3]-);
}
}
}
return ;
}

poj1988_Cube Stacking的更多相关文章

  1. CSS——关于z-index及层叠上下文(stacking context)

    以下内容根据CSS规范翻译. z-index 'z-index'Value: auto | <integer> | inheritInitial: autoApplies to: posi ...

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

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

  3. 关于stacking context和CSS z-index的总结

    HTML中决定元素叠加顺序的CSS属性最有名的应该是z-index了.但是,往往在项目中发现有些情况和我们的预期不太一致.经过研究和学习,总算搞清楚了其中的关系.简单总结如下: 只有Positione ...

  4. UVa 103 - Stacking Boxes(dp求解)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  5. 层叠上下文(The stacking context)

    MDNThe stacking context 层叠上下文是HTML元素的三维概念,这些HTML元素在一条假想的相对于面向(电脑屏幕的)视窗或者网页的用户的z轴上延伸,HTML元素依据其自身属性按照优 ...

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

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

  7. Cube Stacking

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

  8. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

  9. 层叠水平(stacking level)

    运用上图的逻辑,上面的题目就迎刃而解,inline-blcok 的 stacking level 比之 float 要高,所以无论 DOM 的先后顺序都堆叠在上面. 不过上面图示的说法有一些不准确,按 ...

随机推荐

  1. 79 umount-卸除目前挂在Linux目录中的文件系统

    Linux umount命令用于卸除文件系统. umount可卸除目前挂在Linux目录中的文件系统. 语法 umount [-ahnrvV][-t <文件系统类型>][文件系统] 参数: ...

  2. android多线程断点续传下载文件

    一.目标 1.多线程抢占服务器资源下载. 2.断点续传. 二.实现思路. 假设分为三个线程: 1.各个线程分别向服务器请求文件的不同部分. 这个涉及Http协议,可以在Header中使用Range参数 ...

  3. JS获取URL地址参数

    <script> var url = "http://127.0.0.1/index.php?old_quantity=168&date=1478309879000$id ...

  4. 私有项目免费使用Git

    GitHub,私有项目是要收费了,如果想建立私有项目可以选择GitLab. GitLab注册地址:https://gitlab.com/users/sign_in 本机需要安装git,图形化的工具使用 ...

  5. Nginx编译配置杂记

    1.http://nginx.org/download/nginx-1.6.3.tar.gz 2. [root@track nginx-1.6.3]#./configure --prefix=/usr ...

  6. CSS复习

    CSS 选择器 p.into  表示带有into类的p元素 伪类: a)      first-line b)      last-line 伪元素: :before  能在指定的元素前添加内容(创造 ...

  7. 《Python核心编程》 18.多线程编程(一)

    一进程和线程 1参考链接: http://www.zhihu.com/question/25532384 中OF小工和zhonyong 的回答 总结他们两的回答: 引言: 1.电脑的运行,在硬件上是C ...

  8. Linux解压,压缩小总结

    linux下打包与解压的三种命令 最近在读<鸟歌的Linux私房菜基础篇>,想着总结一下所读知识,有益于理解. Linux下常用的命令有三种 gzip,zcat(用于zip,gzip等) ...

  9. 在Excel中使用SQL语句查询和筛选

    本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...

  10. ThinkPHP真正疑难问题笔记

    如何选择线程安全版本还是非线程安全版本: http://www.cnblogs.com/Alight/p/3389113.html(看webserver处理请求时, 使用的是多线程的方式还是 多进程的 ...