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. flowable IdmEngine和IdmEngineConfiguration

    IdmEngineConfiguration 继承了 AbstractEngineConfiguration. 一.创建EngineConfiguration实例 IdmEngineConfigura ...

  2. Kotlin Reference (二) Idioms

    most from reference 一些常用操作 创建单例类 object 数据类data classList.Map.Array的简单操作Lazy延迟加载属性空类型?空类型表达式?..?:.?. ...

  3. matlab linux的安装(第二次)

    1 挂载 2 下载jdk,matlab里面那个不行,缺点东西 3 安装 ./install *** 4 激活,非在线 5 license.lic路径在百度云中有 6 运行路径在你安装过程中的选的路径, ...

  4. 2D game engine essentials [to be continued...]

    All 2D Game Engines/Frameworks are trying to solve the same problem(s). Languages don't matter- they ...

  5. Ubuntu终端远程连接linux服务器

    SSH是一个远程接入软件,可以让你想坐在计算机前面一样操作计算机.SSH使用加密方式传输数据,是一种非常安全的工作方式 步骤如下: 前提:如果没有安装ssh的话请  sudo apt-get inst ...

  6. flask第十三篇——url注意事项

    先和大家说一下常用的请求方法 get:只是从服务器获取数据,不会对服务器的状态或数据产生任何影响: get方法的参数是放在URL中传递的. post:会对服务器的状态或数据产生影响:通过body形式进 ...

  7. jspm 安装试用

    1. 安装 yarn global add jspm or npm install -g jspm 2. 创建项目使用 mkdir appdmeo jspm init 3. 安装依赖 jspm ins ...

  8. 如何查看你的 FastAdmin 服务器是否开启了 gzip br 压缩

    如何查看你的 FastAdmin 服务器是否开启了 gzip 压缩 gzip br 压缩的好处不用多说了. 但是怎么方便的知道网站到底有没有开启或使用了 gzip 呢,其实只要在浏览器就可以看到. 按 ...

  9. 利用 netsh 给 mysql 开启多端口监听

    利用 netsh 给 mysql 开启多端口监听 标题党,实际并不是真的多端口监听,只是端口转发而已. 由于某种特殊原因需要 mysql 服务器多个端口监听. mysql 服务器本身是不支持的,但可以 ...

  10. 用oradebug short_stack及strace -p分析oracle进程是否dead或出现故障

    1,可以采用oradebug或者strace -p跟踪后台或前台进程是否dead或hang住2,如果进程出现故障,必会在对应的TRC文件写入最新信息,基于此可以获取非常重要的信息进一步分析与诊断    ...