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

#include <iostream>
#include<stdio.h>
using namespace std;
#define maxn 30003
int num[maxn];
int dis[maxn]={0};
int father[maxn];
int find_set(int x)
{
    if(x!=father[x])
    {
        int temp=father[x];
        father[x]=find_set(father[x]);
        dis[x]+=dis[temp];
    }
    return father[x];
}
void union_set(int a,int b)
{
    int fathera=find_set(a);
    int fatherb=find_set(b);
    father[fatherb]=fathera;
    dis[fatherb]=num[fathera];
    num[fathera]+=num[fatherb];
}
int main()
{
    int n;
    char ch[2];
    int a,b;
    scanf("%d",&n);
    int i;
    for(i=0;i<maxn;i++)
    {
        father[i]=i;
        num[i]=1;
    }
    while(n--)
    {
        scanf("%s%d",&ch,&a);
        if(ch[0]=='M')
        {
            scanf("%d",&b);
            union_set(a,b);
        }
        else
        {
            int temp=find_set(a);
            printf("%d\n",num[temp]-dis[a]-1);
        }
    }
    return 0;
}

PKU1988磁铁的更多相关文章

  1. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...

  2. 利用智能手机(Android)追踪一块磁铁(二)

    在上一篇博客中提到了利用磁场强度推算传感器位置坐标的公式,下面就介绍怎么利用智能手机完成磁铁的追踪(任何具有磁感应器的装置均可以),这里主要是利用Android手机. 1:程序步骤: 首先将磁铁放置在 ...

  3. 利用智能手机(Android)追踪一块磁铁(一)

    之前看到一个外国人用iPhone做了一个追踪磁铁的Demo感觉不错(参考视频:http://v.youku.com/v_show/id_XODM2MjczNzE2.html),然后我就参考做了一个An ...

  4. 利用智能手机(Android)追踪一块磁铁(转)

    利用智能手机(Android)追踪一块磁铁(一) 利用智能手机(Android)追踪一块磁铁(二) 利用智能手机(Android)追踪一块磁铁(三)

  5. windows phone 8.1开发:磁铁|Tile更新

    原文出自:http://www.bcmeng.com/tile/ 上一篇给大家分享了toast通知操作的方法,这一篇文章我们就来看windows phone 8.1开发中的磁铁更新.磁铁是window ...

  6. MJJ玩磁铁

    题目: Problem D: MJJ玩磁铁 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 139  Solved: 9[Submit][Status][ ...

  7. Win10手动添加开始磁铁

    1.移动到C:\Users\spring\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 2.拖拽

  8. WPF 控件总结

    内容控件:1.Button:当Button.IsCancel="True"时,点击按钮,对话框关闭.当Button.IsDefault="True",按回车触发 ...

  9. Windows phone应用开发[17]-xap提交异常处理

    在windows phone 应用提交操作上早在2011年时就写过一篇Windows phone 应用开发[4]-应用发布,那时wp应用提交官方市场的流程繁杂[超过了5步].因为上传和填写应用信息页面 ...

随机推荐

  1. React中利用axios来实现数据请求

    axios是基于Promise来封装的,通常我们会用axios在数据请求这块作如下配置: 一.拦截器 有注释,不难理解,通常请求头参数不是写死的,应该是去浏览器中读的,例如,login之后返回toke ...

  2. React-Native进阶_7.TextInput的使用实现搜索功能

    前面使用TabBar 实现了底部tab标签,通过stackNavigator 实现了页面跳转,接下来,使用TextInput 来实现一个搜索功能. TextInput 属性比较多,不一一介绍,具体可以 ...

  3. linux vim 命令使用

    基本上vim可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode) 模式切换方法 在命令模式输入“i”,进入插入模式 ...

  4. [Math]Pi(1)

    数学知识忘地太快,在博客记录一下pi的生成. 100 Decimal places 3.14159265358979323846264338327950288419716939937510582097 ...

  5. python2 使用matplotlib

    背景 由於pyh在python3上沒法兒用,所以只能在python2上用pyh2 相應地也要在python2上使用matplotlib 下載 有兩種方法,pip & dnf pip爲: pip ...

  6. HTML编写规范

    HTML和CSS编码规范内容 一.HTML规范 二.CSS规范 三.注意事项: 四.常用的命名规则 五.CSS样式表文件命名 六.文件命名规则 一.HTML规范: 1.代码规范 页面的第一行添加标准模 ...

  7. Jenkins构建持续集成

    Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变得可能.现在软件开发追求的是效率以及质量,Jenkins使得自动化成为可能! 亮点 采用shell自定义脚本,控制 ...

  8. ios一些噁心记录

    有时在tableview的头部会凭空多出一块空白区域,这是由于ios会"贴心"的多分配一些用于滑动的多余inset. 消除这一空白的方法是,在tableview所在的control ...

  9. 安装hadoop 2.2.0

    安装环境为 CentOS 64位系统, 大概分下面几个步奏, 0. 安装JDK1. 配置SSH2. 配置/etc/hosts3. 拷贝hadoop包到没台机器上4. 修改hadoop配置文件5. 关闭 ...

  10. softmax与logistic关系

    Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值. Softmax回归模型对于诸如MNIST手写数字分类等问题是很有用的,该问 ...