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. 【跟着子迟品 underscore】如何优雅地写一个『在数组中寻找指定元素』的方法

    Why underscore (觉得这部分眼熟的可以直接跳到下一段了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...

  2. 安卓 service

    public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Inte ...

  3. .net项目在linux平台的CI流程(基于Jenkins+mono+jexus)

    内容较多,主要分为以下4方面内容: Jenkins的安装部署(centos 7+) .net在linux平台CI流程所需的插件管理&配置 Jenkins配置连接Gitlab(也可使用对应插件连 ...

  4. Asp.Net MVC中Action跳转小结

    首先我觉得action的跳转大致可以这样归一下类,跳转到同一控制器内的action和不同控制器内的action.带有参数的action跳转和不带参数的action跳转. 一.RedirectToAct ...

  5. Alpha阶段第十次Scrum Meeting

    情况简述 Alpha阶段第十次Scrum Meeting 敏捷开发起始时间 2016/11/3 00:00 敏捷开发终止时间 2016/11/4 00:00 会议基本内容摘要 详细定义了API接口,汇 ...

  6. php网址显示excel表格内容

    /** * excel表格内容在网页中显示 * * 首先需要下载PHPExcel 工具包 * 网址: http://phpexcel.codeplex.com/releases/view/119187 ...

  7. 11月8日PHP练习《留言板》

    一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php  登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  8. yii2 composer安装

    安装Yii2 1.安装composer 在命令行输入 curl-sS https://getcomposer.org/installer | php mv composer.phar /usr/loc ...

  9. tnt_esri.dat Arcgis8.1安装license

    arcgis8.1授权文件内容,复制个txt重命名为tnt_esri.dat即可.注意替换yourcomputername为你的计算机名   SERVER yourcomputername ESRI_ ...

  10. 面试题目——《CC150》智力题

    面试题6.1:有20瓶药丸,其中19瓶装有1克/粒的药丸,余下一瓶装有1.1克/粒的药丸.给你一台称重精准的天平,怎么找出比较重的那瓶药丸?天平只能用一次. 思路:第1瓶取1颗,第2瓶取2颗....最 ...