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. jsp页面图片显示不出来

    jsp页面就是MyJsp.jsp <body> This is my JSP page. <br> <img src="img/top.jpg"> ...

  2. [AirFlow]AirFlow使用指南四 DAG Operator Task

    1. DAG 在Airflow中,DAG或有向无环图是你运行所有任务的集合,以某种组织方式来反映所有任务之间的关系和依赖. 例如,一个简单的DAG可以包括三个任务:A,B和C.可以说A必须在B运行之前 ...

  3. 单一功能学习——百度AI之身份证识别

    以下内容基本是从官方接口文档复制过来的,附带自己封装的代码 时间:2018年4月4日 一.导入工具包 使用maven依赖: <dependency> <groupId>com. ...

  4. 实战maven私有仓库三部曲之二:上传到私有仓库

    在上一章<实战maven私有仓库三部曲之一:搭建和使用>我们搭建了maven私有仓库,并体验了私有仓库缓存jar包的能力,避免了局域网内开发人员去远程中央仓库下载的痛苦等待,本章我们再来体 ...

  5. python中利用类创建的对象来保存信息

    在类创建的对象中,一般都是以字典的方式来保存信息 class Student: def __init__(self, name, age, score): self.name = name self. ...

  6. BZOJ - 3295 动态逆序对 (树状数组套treap)

    题目链接 思路和bzoj2141差不多,不过这道题的数据更强一些,线段树套treapT了,树状数组套treap卡过~~ #include<bits/stdc++.h> using name ...

  7. 每天一个linux命令(性能、优化):【转载】free命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  8. BZOJ4518 Sdoi2016 征途 【斜率优化DP】 *

    BZOJ4518 Sdoi2016 征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m ...

  9. simulink使用system test测试 (matlab2014a)

    simulink中有个工具system test,tools->systemtest->launch system test 打开.打开之后界面是这样的 选择insert->test ...

  10. 关于matlab中定点数overflow的处理办法

    定点数overflow的处理有两种办法:1,saturate,也就是说如果超过定点的最大值就取最大值,例如最大值是6结果是8,那么就取6:2,wrap,就是循环,如下图所示